Random Number Generation....

Started by
1 comment, last by Grellin 22 years, 7 months ago
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 */
Steven Bradley .:Personal Journal:. .:WEBPLATES:. .:CGP Beginners Group:. "Time is our most precious resource yet it is the resource we most often waste." ~ Dr. R.M. Powell
Advertisement
Don''t continuously seed the random number generator:
// insert your Roll() definition here.int main( .. ){  srand((unsigned)time(NULL));  ...  while( condition)  {    ...    r = Roll();    ...  }} 
Thanks so much! Now I can put the rest of my Tylenol away!

/* GRELLIN */
Steven Bradley .:Personal Journal:. .:WEBPLATES:. .:CGP Beginners Group:. "Time is our most precious resource yet it is the resource we most often waste." ~ Dr. R.M. Powell

This topic is closed to new replies.

Advertisement