Sign in to follow this  
sayallenthing

need a random number in c++

Recommended Posts

I was looking in the noteworthy threads, and one of them was programs for beginners to practise, and one was, to have the program pick a number and the user guesses it, and it tells the user if it's higher or lower, and so on. So I said, sure, I'll do that... Then as soon as I started I'm like "I just need to have it pick a number, then I realized I wanted this to be entertaining, and not have it pick a predefined number, so I had trouble finding out how to do random numbers. The best I have is "rand()" whenever I get it to do that, it is always the same numbers, I know why, but what I don't know is how to fix it. Any help would be appreciated

Share this post


Link to post
Share on other sites
srand() seeds the random number generator (seed random). time(0) returns a time_t representing the number of seconds since the unix epoch. This makes sure the random number generator algorithm always starts at a different spot.

Share this post


Link to post
Share on other sites
In case you are wondering, rand() as is the case for all non-hardware based random number generators is psuedo-random and deterministic - it is possible to predict the next number knowing the seed and by analyzing the generated patterns. The difficulty of this decides how useful the PRNG is to encryption methods.

The number generator, depending on method creates a sequence of numbers that attempt to emulate the behaviour of real random phenomena. Most of them fail to do so well in the area of distribution and uniformity. To initiate the psuedo-random number generator a seed is often given which affect the numbers generated, essentially where in the sequence you are starting. For simple cases it is usually sufficient to seed the PRNG with the current time. Although this is not a problem for many people, it is important to realize that due to the memory limitations PRNG are periodic. After a certain number of iterations they repeat.

Share this post


Link to post
Share on other sites
So, to clarify the above responses a bit, you'll want to call srand() once at the beginning of your program. It accepts a seed value as an argument, and the time is often a good option for this, at least when working with fairly simple programs like this. Then use rand() as normal in your program, and you should get a different sequence of numbers most of the time when you run your program.

If you wanted a source example, you could try reading through Generating Random Numbers [www.cprogramming.com].

//EDIT: Beaten by Daerax, his reference looks a little clearer - that'll teach me to keep things open in multiple tabs before responding.

Share this post


Link to post
Share on other sites
In addition, finding a Mersenne Twister psuedo-random number generator library (I say library, it's really only one file usually) out there is pretty easy, because C's rand() is generally implemented pretty horribly in terms of "randomness". In fact, a quick good leads me to this well developed one. Same concept - seeding and calling rand.

Hope that helps.

Edit: Although for the sake of good C++, change the #includes to the proper <cmath>, <ctime>, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this