C++11 comes with a set of PRNG's.
In what situation should one choose one over another? What are their advantages, disadvantages etc.
rand() The function rand() is used for random number generator in C in a certain range that can take values from [0, Range_max]. Range_max value can be an integer. It does not take any parameters, and it returns random numbers.
Random Number Generator (Pseudocode)RANDOM(Integer1 : INTEGER, Integer2 : INTEGER) RETURNS INTEGER generates a random integer in the range from Integer1 to Integer2 inclusive.
In this program we call the srand () function with the system clock, to initiate the process of generating random numbers. And the rand () function is called with module 10 operator to generate the random numbers between 1 to 10. srand(time(0)); // Initialize random number generator.
Random numbers are widely used in encryption and security applications, usually to generate encryption keys or secret data to be shared between communication entities. Therefore a Random Number Generator (RNG) is a very important primitive for cryptographically secure applications [2].
I think the Mersenne twister std::mt19937
engine is just fine as the "default" PRNG.
You can just use std::random_device
to get a non-deterministic seed for mt19937
.
There is a very interesting talk from GoingNative 2013 by Stephan T. Lavavej:
rand()
Considered Harmful
You can download the slides as well from that web site. In particular, slide #23 clearly compares mt19937
vs. random_device
:
mt19937
is:
- Fast (499 MB/s = 6.5 cycles/byte for me)
- Extremely high quality, but not cryptographically secure
- Seedable (with more than 32 bits if you want)
- Reproducible (Standard-mandated algorithm)
random_device
is:
- Possibly slow (1.93 MB/s = 1683 cycles/byte for me)
- Strongly platform-dependent (GCC 4.8 can use IVB RDRAND)
- Possibly crypto-secure (check documentation, true for VC)
- Non-seedable, non-reproducible
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With