Question

C++11 comes with a set of PRNG's.

In what situation should one choose one over another? What are their advantages, disadvantages etc.

Was it helpful?

Solution

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

OTHER TIPS

The trade-off is speed, memory foot-print and period of PRNG.

  1. Linear Congruential Generators: fast, low memory, small period

  2. Lagged Fibonacci(Subtract with Carry): fast, large memory, large period

  3. Mersenne Twister: slow, very large memory, very large period

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top