Random Number Generation....
Hi all, I am working on a small program and have run in to a problem. I am using this code:
#define MAX 99
int main()
{...}
int Roll()
{
srand( (unsigned)time( NULL ) );
roll = rand()%MAX+1;
cout << roll; // this line only for testing
return roll;
}
to generate a random number. Where I am using it is inside a for loop that is also working. The problem I am having and it is more than likely my own stupidity, is the program goes through the cycles so fast it is returning duplicate numbers. I included a Sleep(500); to test it and it would only send back 2 of the same number before getting a new random number. My question is this, is there another way to seed the rand that will return random integers? Thanks in advance for any help.
/* GRELLIN */
Don''t continuously seed the random number generator:
// insert your Roll() definition here.int main( .. ){ srand((unsigned)time(NULL)); ... while( condition) { ... r = Roll(); ... }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement