如何从C++<random>获得一致的行为
How to get consistent behavior from C++ <random>
我遇到了一个问题,如果我从C++ <random>
库中重新设定随机数生成器的种子,我有时会从序列中获得即将到来的值作为第一个样本。在第一个样本之后,我得到了一个可重复的序列。它似乎有一个模式,但我不能完全弄清楚它是什么。
最小示例:
#include <iostream>
#include <random>
using namespace std;
int main(){
mt19937 engine {1};
normal_distribution<float> nd {0, 1};
for (int i=0; i<10; i++){
for (int j=0; j<=i; j++) {
cout << nd(engine) << endl;
}
cout << endl;
engine.seed(1);
}
return 0;
}
使用 g++ (Ubuntu 7.3.0-27ubuntu1~18.04( 编译,在 WSL Ubuntu 18.04.2 上没有任何标志。
我得到以下输出:
0.3064
0.156066
0.3064
0.156066
0.3064
0.156066
0.3064
0.156066
-0.424386
-0.56804
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
-0.428738
-1.20004
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
-0.428738
-1.20004
1.30547
-1.18775
0.3064
0.156066
-0.424386
-0.56804
-0.204547
-0.806289
-0.428738
-1.20004
1.30547
我希望 0.3064 始终是我得到的第一个值。我可以通过在重新播种后烧掉样品来解决此问题,但我没有看到何时需要这样做的明确模式。有谁知道我为什么会这样做?是否有我应该使用的编译器标志?
您忘记重置发行版的状态。播种发动机后呼叫nd.reset();
。
原始代码在此处复制,并带有修复程序:
#include <iostream>
#include <random>
using namespace std;
int main(){
mt19937 engine {1};
normal_distribution<float> nd {0, 1};
for (int i=0; i<10; i++){
for (int j=0; j<=i; j++) {
cout << nd(engine) << endl;
}
cout << endl;
engine.seed(1);
nd.reset();
}
return 0;
}
带输出的实时示例
相关文章:
- <random>在实践中应该实际使用哪个随机数引擎? std::mt19937?
- 是否可以为boost::random::uniform_int_distribution<>设置确定性种子?
- 'generate'不是窗口头文件中'std::random_device'的成员<random>
- 数组打印"random"值时出现问题
- 使用""的不可重现的随机数<random>
- <random> 尽管不是常量,但所有发行版都是线程安全的吗?
- 从时间生成'random'数字?
- 无法在 c++ 中包含"random"头文件
- Nemiver 找不到文件 /build/glibc-LK5gWL/glibc-2.23/stdlib/random.c
- 如何在 c++ 中正确读取 GRC 块"Random Source"的字节输出数据到自己的 OOT 块中?
- 如何从C++<random>获得一致的行为
- 为什么 random() 在窗口中不起作用
- 等效于C++中的numpy.random.choice函数
- Arduino 的新手。 "Random"值在复位时重复
- 在 c++ 中反转数组时,为什么在输入和输出之间得到一个"random"数字?
- 如何使用<random>填充标准::数组
- stdlib.h's random() on MS Visual Studio 2008
- Boost::random::discrete_distribution构建后如何更改权重
- 使用 和 函数生成随机双向量<random>
- 程序在每次执行时生成相同的'random'编号