Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Pseudo-Random Number Generator to use in C++11?

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

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

like image 893
user877329 Avatar asked Mar 01 '14 08:03

user877329


People also ask

What random number generator does C use?

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.

How do I generate a random number in pseudocode?

Random Number Generator (Pseudocode)RANDOM(Integer1 : INTEGER, Integer2 : INTEGER) RETURNS INTEGER generates a random integer in the range from Integer1 to Integer2 inclusive.

How do you generate a random number between 1 and 10 in C?

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.

What cryptographically strong pseudo-random number generator is used for?

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].


1 Answers

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
like image 132
Mr.C64 Avatar answered Oct 14 '22 14:10

Mr.C64