Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if std::random_device is in fact random?

Tags:

c++

random

c++11

Quoting from cppreference:

std::random_device is a non-deterministic random number engine, although implementations are allowed to implement std::random_device using a pseudo-random number engine if there is no support for non-deterministic random number generation.

Is there a way to check whether current implementation uses PRNG instead of RNG (and then say exit with an error) and if not, why not?

Note that little bit of googling shows that at least MinGW implements std::random_device in this way, and thus this is real danger if std::random_device is to be used.

---edit---
Also, if the answer is no and someone could give some insight as to why there is no such function/trait/something I would be quite interested.

like image 628
Xarn Avatar asked Jan 11 '15 11:01

Xarn


1 Answers

Is there a way to check whether current implementation uses PRNG instead of RNG (and then say exit with an error) and if not, why not?

There is a way: std::random_device::entropy will return 0.0 if it is implemented in terms of a random number engine (that is, it's deterministic).

From the standard:

double entropy() const noexcept;

Returns: If the implementation employs a random number engine, returns 0.0. Otherwise, returns an entropy estimate for the random numbers returned by operator(), in the range min() to log_2(max() + 1).

like image 77
Joseph Mansfield Avatar answered Nov 13 '22 19:11

Joseph Mansfield