The new C++11 Standard has a whole chapter dedicated to random number generators. But how do I perform the simplest, most common task that used to be coded like this, but without resorting to the standard C library:
srand((unsigned int)time(0)); int i = rand();
Are there reasonable defaults for random-number engines, distributions, and seeds that one could use out of the box?
You should be able to do something like:
The quality of the
default_random_engine
is implementation dependent. You could also usestd::min_rand0
orstd::min_rand
.Probably a better way to seed a random engine is with as true a random number as is available from the implementation rather than use
time
.E.g.
I use the following code in my project. 'engine' and 'distribution' can be one of the provided by the library.
Random number generation is a difficult problem. There is no truly random way to do it. If you are just generating randomness to seed a game environment then your approach should be fine. rand() has several shortcomings.
If you are needing randomness to generate encryption keys then you're S.O.L. The best way in that case is to go out to the operating system, which usually has mechanism. On POSIX that's random() (or read from /dev/random if you're so disposed). On Windows you can use the CryptoAPI:
https://www.securecoding.cert.org/confluence/display/seccode/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers
If your existing code was appropriate before the new standard, then it will continue to be. The new random number generators were added for applications which require a higher quality of pseudo-randomness, e.g. stochastic simulation.
You could use RC4 to generate random bytes. This probably has the properties that you want. It is fast and fairly simple to implement. The sequence is repeatable across all implementations when the seed is known, and completely unpredictable when the seed is not known. http://en.wikipedia.org/wiki/RC4
Unifying and simplifying some of the samples already provided I will summarize to:
Then I ran some tests to see what the defaults look like:
Produces the output:
So as we can see, mt19937 and default_random_engine have a different default range, so use of uniform_int_distribution is advised.
Also, default uniform_int_distribution is [0, max_int] (non-negative), even when using a signed integer type. Must provide range explicitly if you want full range.
Finally, its important to remember this at times like these.