Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

Random numbers

This topic is 4494 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

How would I create a number randomizer that would create a random number each time I ran the program, because the one I have now creates the number but's it's always the same one.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Kenny77
How would I create a number randomizer that would create a random number each time I ran the program, because the one I have now creates the number but's it's always the same one.
It depends. In programming you're usually dealing with PRNGs (pseudo-random number generators), so the question isn't so much how to generate a random number but how to make it appear random.

The most easily accessible PRNG (assuming C or C++) is probably the rand() function. This function generates a series of pseudo-random numbers; the series is determined by the seed, which you can set with srand(). The appearance of randomness can usually be accomplished (for all practical purposes) by calling srand() with a value that is unlikely to be the same between executions. Quite often a value derived from the system clock is used for this.

If you want more sophisticated capabilities, you might look into Boost, which I believe has some PRNG classes.

Share this post


Link to post
Share on other sites
Yeah, srand(time()), rand() % MAX_RESULT is about the best way, there are people that layer things and use loops and all sorts of things to get "more random" numbers, but the real nitty-gritty is, you're using a finite system, so there is of course, no way to truly just pick a number out of thing air.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!