Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Random Number Generation....

This topic is 6084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 */

Share this post


Link to post
Share on other sites
Advertisement
Don''t continuously seed the random number generator:

// insert your Roll() definition here.

int main( .. )
{
srand((unsigned)time(NULL));
...
while( condition)
{
...
r = Roll();
...
}
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!