且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

随机数生成器始终生成相同的数字

更新时间:2022-01-03 09:10:07

调用 entropy()成员函数,以了解您的实现是否正确实现:

Call the entropy() member function on std::random_device to find out whether your implementation implements it properly:

std :: random_device可以根据实现定义的伪随机数引擎,如果非确定性来源(例如硬件设备)不可用于实施.在这种情况下,每个std :: random_device对象都可以产生相同的数字序列.

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

()

如果是这种情况,请调用 entropy()将返回 0 :

If this is the case, a call to entropy() will return 0:

确定性随机数生成器(例如伪随机引擎)熵为零.

A deterministic random number generator (e.g. a pseudo-random engine) has entropy zero.

在这种情况下,您需要使用其他回退机制进行播种.例如,您可以像以前的C天一样使用基于时间的种子.

If that is the case, you need to use a different fallback mechanism for seeding. For instance, you could use a time-based seed like in the old C-days.

尤其是在台式机平台上,您应该希望将 std :: random_device 实施为适当的不确定源.如果不是这种情况,则您可能只是在使用标准库实现的旧版本.如果您认为该实现应支持不确定的 std :: random_device ,但不支持,请考虑向您的标准库维护者提交错误报告.

On desktop platforms in particular, you should expect std::random_device to be implemented as a proper non-deterministic source though. If this is not the case, you might just be using a very old version of the standard library implementation. If you have the feeling that the implementation should support non-deterministic std::random_device but it does not, consider filing a bug report with your standard library maintainer.