• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Random Number from 0 to X

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
strickdd
Asked:
strickdd
  • 2
1 Solution
 
rstaveleyCommented:
0
 
bastibartelCommented:
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
 
bastibartelCommented:
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now