C++11 comes with a set of PRNG's.
In what situation should one choose one over another? What are their advantages, disadvantages etc.
C++11 comes with a set of PRNG's.
In what situation should one choose one over another? What are their advantages, disadvantages etc.
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:
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
The trade-off is speed, memory foot-print and period of PRNG.
Linear Congruential Generators: fast, low memory, small period
Lagged Fibonacci(Subtract with Carry): fast, large memory, large period
Mersenne Twister: slow, very large memory, very large period
© 2022 - 2024 — McMap. All rights reserved.