我一直在使用random_device rd{}为 Mersenne-Twister 伪随机数生成器生成种子,mt19937 RNG{rd()} 正如此处所建议的那样。然而,文档中写道(文档示例代码中的注释),“random_device一旦熵池耗尽,许多实现的性能都会急剧下降。在实际使用中,random_device通常仅用于播种 PRNG,例如mt19937”。我尝试测试这个“熵池”有多大,对于 10^6 次调用,random_device返回超过 10^2 个重复数字(请参阅下面的示例代码和输出)。换句话说,如果我尝试将random_device其用作 Mersenne-Twister PRNG 的种子,它将生成大量重复种子。
问题:人们是否仍在使用random_deviceC++ 来生成 PRNG 种子,或者是否已经有更好的替代方案?
我的代码:
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
int main(){
auto begin = std::chrono::high_resolution_clock::now();
random_device rd{};
mt19937 RNG{ rd() };
int total_n_of_calls = 1e6;
vector<int> seeds;
for(auto call = 0; call < total_n_of_calls; call++){
int call_rd = rd();
seeds.push_back(call_rd);
}
int …Run Code Online (Sandbox Code Playgroud)