What is the difference between using std::random_device with pRNG e.g. std::mt19937 and without?
Asked Answered
Z

2

5

In C++11 one can generate numbers with the use of std::random_device with or without a pseudo random number generator like mt19937.

What will be the difference using this in this exemplar code:

#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(1, 10);

    for (int i=0; i<16; ++i)
        std::cout << dist(rd) << "\t" << dist(mt) << "\n";
}
Zack answered 16/11, 2014 at 20:37 Comment(1)
A
4

std::random_device is supposed to get you a seed for engines like mt19937. The quality of successive numbers produced is completely undefined and may easily be insufficient for practical purposes (such as cryptography), so relying on that is out of question.

Apart from that, mt19937 will give you the same sequence when given the same seed. A random_devices values can be only influenced by the string given to its constructor... which implies implementation-defined behavior.

Ashleaashlee answered 16/11, 2014 at 20:42 Comment(0)
C
1

There are two differences that I know of:

  1. Using mt199937 will be faster but less cryptographically secure.
  2. std::random_device will always be random, but if you initialize your mt19937 with a constant seed it will always give you the same random numbers:

    std::mt19937 mt(2014);

Will give the same sequence of random bits every time. This can be useful if you want to test a specific behavior over and over again. The standard requires this in 26.5.5/4:

Required behavior: The 10000th consecutive invocation of a default-constructed object of type mt19937 shall produce the value 4123659995.

There is no such equivalent consistency with std::random_device.

Conciliate answered 16/11, 2014 at 20:45 Comment(8)
@KarolyHorvath Yeah technically only for default-construction, but that's not at all a requirement for random_device and that's the only thing I see for the prng...Conciliate
and does only mention one value. weird. (note: you meant mt19937).Flummox
I mean - there's no requirement on what the nth value of random_device is. Whereas, given a seed, there is a specific sequence of values that mt19937 will give you. Even though that's apparently not in the standard.Conciliate
If you have a suggestion for how to phrase my answer better, I'm all for it :)Conciliate
@Barry, what do you mean by "std::random_device will always be random"? Is it guaranteed to use external source of randomness, like heat dissipation/mouse clicks/movement etc? AFAIK, the standard does not guarantee "true" randomness even for "std::random_device". In case the system has ability for true rng, it will use it, but otherwise no, correct me if I'm wrong.Oldcastle
@Oldcastle You are correct. The standard states (26.5.6/1-2): A random_device uniform random number generator produces non-deterministic random numbers. If implementation limitations prevent generating non-deterministic random numbers, the implementation may employ a random number engine."Conciliate
@Barry, thanks, that's what I remembered, and this is a serious issue in crypto programs, as getting true randomness is a very very tricky issueOldcastle
I think that by "less cryptographically secure" you mean "not cryptographically secure".Pathological

© 2022 - 2024 — McMap. All rights reserved.