Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random Number from 0 to X

Posted on 2006-07-05
3
Medium Priority
?
270 Views
Last Modified: 2010-04-01
I need a random number generator that will give me a different number each time it is executed. The purpose of this is to insert random FLOAT numbers into an array.
0
Comment
Question by:strickdd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 1500 total points
ID: 17043402
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17043526
Hi there,

//** a = [0...1[  
double a = (double)(rand()%RAND_MAX) / RAND_MAX

will generate pseudo random floating point values between 0 and 0.999...
There are better random number generators than the standard c implentation.

Get back if you have higher demands on the randomness.

Cheers,
Sebastian

0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17043554
one addition:

The rand() function wll provide the same sequence of random integer values [0...RANDMAX] whenever your application restarts.
Use srand((unisnged int) time());
to intialize ('seed') the random number sequence.


Cheers,
Sebastian
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question