c++std::normal_distribution在从文件恢复时给出不一致的随机数
c++ std::normal_distribution gives inconsistent random numbers when restoring from file
我正在编写一个蒙特卡罗模拟并实现了检查点。我想获得完全相同的结果,无论我是否从检查点重新启动模拟或继续超过它。然而,我在std::normal_distribution
:中遇到了一些奇怪的行为
我使用std::mt19937 rng;
作为RNG,并将其种子设定为一个固定的数字。我通过CCD_ 3和CCD_。然后,我将rng的状态写入ofstream os
:
os << rng << endl;
os << <some other stuff>...
紧接着,我又画了几个数字:
os << uniform(rng) << endl;
os << uniform(rng) << endl;
os << uniform(rng) << endl;
os << normal(rng) << endl;
os << normal(rng) << endl;
os << normal(rng) << endl;
os << uniform(rng) << endl;
os << uniform(rng) << endl;
os << uniform(rng) << endl;
我得到以下输出:
0.727133
0.215537
0.516879
-2.12532
0.314652
1.78136
0.511111
0.83119
0.637067
然而,如果我从检查点重新启动,即从ifstream is
:初始化生成器
is >> rng;
is >> <some other stuff>...
并画出相同的9个随机数(3个均匀,3个正常,3个均匀),我得到:
0.727133
0.215537
0.516879
0.314652
1.78136
1.28201
0.637067
0.298175
0.802607
你看,统一的数字是相同的,直到画出一个正常的数字,之后rng的状态不同。gdb
的介入证实了这一点。
查看0.637067
在两个输出中的位置。你会注意到,正态分布在恢复时必须从rng中提取比未恢复时更多的数字。这是因为当你检查点的时候它有熵。
您必须保存或重置normal
的状态。我建议在normal
上调用reset作为检查点过程的一部分。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 整数不会重复超过随机数
- 用C++将哈希表写入文件并从文件中恢复
- if数组上的随机数
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 半随机数生成C++
- 程序在尝试猜测它选择的随机数时进入无限循环?
- Opencv 恢复到比我设置的更高的分辨率
- 有没有办法在 c++ 中同时生成随机数?如果没有,是否有解决方法?
- 变量在使用赋值语句赋值后恢复为以前的值
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 具有随机数的二维数组不会更改
- 随机数未达到限制
- 我想生成许多矩阵并用随机数填充它
- <random>在实践中应该实际使用哪个随机数引擎? std::mt19937?
- 制作具有平均值的随机数生成器
- 删除所有字符串后如何恢复 QStringList?
- 为什么 rand 不在我的代码中生成随机数?
- 如何使用CUDA CURAND保存和恢复随机数生成器的状态
- c++std::normal_distribution在从文件恢复时给出不一致的随机数