Public Group

# need a random number in c++

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

## 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

srand(time(0));

##### 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 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 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 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 on other sites
boost::random has all you need (several generators and distributions) and quite good documentation too.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×