How do I generate a random number in C++ with a set range?,
Posted 16 July 1999 - 06:22 PM
to seed you number in you initialization and then whenever you want a new number you would call
int newrandomnum = rand() % num;
This will produce a number from 0 to num - 1.
You have to #include <time.h> for time and I think stdlib.h for the rand() though I'm not to sure.
Big Daddy Scat
Posted 19 July 1999 - 05:51 PM
Members - Reputation: 122
Posted 22 July 1999 - 04:03 AM
the rand()%x function only uses lower-order bits and will generate a much LESS random number( Numbers are never completely random in computers ) then the first method.
Yeah it seems much more complex, but just through it in a function.
On the otherhand if you don't really care how random the numbers really are and thats not important to you, the simpler way will work.
Midnights Dawning Software
Posted 30 July 1999 - 10:55 PM
The following macro will return a random number between min(inclusive) and max(exclusive):
#define randomRange(min, max) ( (int)(min + (max-min)*rand()/(RAND_MAX+1.0)) )
randomRange(1, 10) will then return a random integer between 1 and 10. Regards, White Fire
You will also need to #include
Posted 30 July 1999 - 10:58 PM