gcc implementation std::random_device
对我来说似乎很奇怪。具体来说,第 137 行:
random_device::result_type
random_device::_M_getval()
{
#if (defined __i386__ || defined __x86_64__) && defined _GLIBCXX_X86_RDRAND
if (!_M_file)
return __x86_rdrand();
#endif
假设所有这些数量都已定义。然后,当我在 std::random_device
实例上调用调用运算符时,我从 rdrand
指令中获得了结果。但是 rdrand
没有实现随机设备; rdrand
是一个伪随机数生成器,比物理源或随机性更接近(比如说)梅森扭曲器。
我希望在这里调用 rdseed
buildin/intrinsic,因为 rdseed
查询通过 thermal noise 生成随机数的电路.这对我来说更像是一个“设备”。
但我对随机数生成了解不多;那么我关于 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 eachstd::random_device
object may generate the same number sequence.—
std::random_device
, C++ Reference
我不能说 GCC 决定在宏计算结果为 true 的情况下以这种方式实现 random_device
,但考虑到 if(!_M_file)
检查,我的猜测是 _M_file
是对什么构成潜在熵源的引用,如果没有它,GCC 将回退到常规伪随机设备。
关于c++ - gcc 对 `std::random_device` 的实现是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246203/