有没有办法检查 std::random_device 是否实际上是随机的?

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

引用自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.

有没有办法检查当前的实现是否使用 PRNG 而不是 RNG(然后说错误退出),如果没有,为什么不呢?

请注意,一点谷歌搜索表明至少 MinGW 以这种方式实现了 std::random_device,因此如果要使用 std::random_device,这是真正的危险。

---编辑---
另外,如果答案是否定的,并且有人可以就为什么没有这样的 function/trait/something 给出一些见解,我会很感兴趣。

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?

有一个办法:std::random_device::entropy如果是用随机数引擎实现的话std::random_device::entropy会return0.0

来自标准:

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

没有 100% 安全的方法来确定真正的随机性。使用黑盒方法,你能做的最好的事情就是展示证据,如果它不是完全随机的:

  • 首先,您可以通过生成大量随机数字并对它们的分布进行统计(例如,生成 0 到 1000 之间的 100 万个随机数)来验证分布是否随机。如果某些数字出现的频率明显高于其他数字,那么显然它并不是真正随机的。

  • 接下来您可以 运行 程序多次在相同的初始种子后生成随机数。如果您获得相同的随机数序列,那么它肯定是 PRNG 而不是真正的随机性。但是,如果您没有获得相同的序列,它不会证明任何事情:库可以使用某种自动种子(使用时钟滴答或其他东西)来 hide/improve 伪随机性。

  • 如果您的应用程序高度依赖随机性质量(例如密码质量),您应该考虑更多测试,例如 NIST SP 800-22

  • 推荐的那些