我正在玩一些 C++ 随机引擎,有些事情让我很不安。 注意到我所拥有的值大致具有相同的顺序,我进行了以下测试:
#include <random>
#include <functional>
#include <iostream>
int main()
{
auto res = std::random_device()();
std::ranlux24 generator(res);
std::uniform_int_distribution<uint32_t> distribution;
auto roll = std::bind(distribution, generator);
for(int j = 0; j < 30; ++j)
{
double ssum = 0;
for(int i = 0; i< 300; ++i)
{
ssum += std::log10(roll());
}
std::cout << ssum / 300. << std::endl;
}
return 0;
}
无论我使用什么引擎,我打印的值都在 9.2 左右,看起来更像是正态分布。 有什么我没有正确理解的吗? 谢谢, 纪尧姆
最佳答案
Having noticed that the values I had were roughly of the same order
这正是您对统一随机数生成器的期望。 [10^(n-1),10^n) 范围内的整数是 [0,10^(n-1)) 范围内整数的 9 倍。
关于C++随机引擎不是真正随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38205724/