A continuous random number generator?
-
rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.
-
rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.
avimitrani wrote:
is there no escape from calling rand() twice and then multiplying the results?
That's not really random, nor does it give a wide enough range. Try calling it twice and shifting the first result to take the bits above the second result.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.
You could generate a random number between 0 and 1, then multiply that by the bigger number you want to use, e.g.
flaot fRand = rand() / (float)RAND_MAX; // Generate a random number between 0 and 1.
int iValue = fRand * 10000000; // Generate a random number between 0 and 10000000 (where 10000000 if your new max value).
-
You could generate a random number between 0 and 1, then multiply that by the bigger number you want to use, e.g.
flaot fRand = rand() / (float)RAND_MAX; // Generate a random number between 0 and 1.
int iValue = fRand * 10000000; // Generate a random number between 0 and 10000000 (where 10000000 if your new max value).
That won't work: some values might have no chances at all to be picked.
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
That won't work: some values might have no chances at all to be picked.
Cédric Moonen Software developer
Charting control [Updated - v1.1]Exactly!
-
rand() returns an integer in the range [0 - RAND_MAX]. What if I want to generate random integers in a wider range? is there no escape from calling rand() twice and then multiplying the results? Is there a continuous random number generator in C++ ? Thanks, Avi.
avimitrani wrote:
What if I want to generate random integers in a wider range?
You'll need to roll your own. Something like this.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb