4

I am new in Cryptography and stuff-related to. I have been studying PRNG and CPP libraries which provide these facilities that I find std::random_device and std::mt19937_64 in modern CPP. However, What is the difference between them and How/Where we should use them (in what situation exactly)?

Mike van Dyke
  • 2,724
  • 3
  • 16
  • 31
night-wolf
  • 127
  • 9
  • 1
    See https://stackoverflow.com/questions/53040940/why-is-the-new-random-library-better-than-stdrand and https://stackoverflow.com/questions/38367976/do-stdrandom-device-and-stdmt19937-follow-an-uniform-distribution for potential duplicates. – Max Langhof Nov 13 '19 at 09:45
  • Depending on the implementation, std::random_device may produce cryptographically secure random numbers. std::mt19937 never produces cryptographically secure random numbers. You can seed std::mt19937 and even serialize its internal state. You can't do that with std::random_device. std::mt19937 is generally much faster than std::random_device. – user515430 Nov 13 '19 at 23:33

1 Answers1

4

std::random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers. It could be used with distribution to generate random numbers, but the performance of many implementations of std::random_device degrades sharply once the entropy pool is exhausted, so it is recommended to use it only to seed a pseudo-random number generator such as std::mt19937_64. They are "not so random", but do not degrate as former one, so they are often used in chain like that:

std::random_device rd;
std::mt19937_64 eng(rd());
std::uniform_int_distribution<int> uniform_dist(1, 6)

std::cout << uniform_dist(eng);
Yuri Kovalenko
  • 1,325
  • 9
  • 19