我遇到了一个关于随机数生成器状态的问题。具体来说,保存的引擎状态一旦恢复,就会生成一个稍微偏离的随机数序列。在所有情况下,恢复的序列似乎都向上或向下移动了一个。例如:
1. Create bivariate generator with an MT19937 engine and a standard uniform distribution.
2. Generate some random normal variables.
3. Save the state of the engine. I'll call it state 1.
4. Generate and keep track of the next 5 random normal variables.
5. Repeat steps 2 to 4 to obtain a state 2 and then a state 3.
观察结果(仅供说明):
State 1
Actual Restored state (appears to have shifted up)
a b
b c
c d
d e
e f
State 2
Actual Restored state (appears to have shifted down)
j i
k j
l k
m l
n m
State 3
Actual Restored state (appears to have shifted up again)
r s
s t
t u
u v
v w
代码:
boost::mt19937 engine(seed);
boost::normal_distribution<double> dd(0, 1);
boost::variate_generator<boost::mt19937&, boost::normal_distribution<double>> rr(engine, dd);
std::stringstream ss1; std::stringstream ss2; std::stringstream ss3;
for (long p=0; p<20; p++) {rr();}
ss1 << engine;
for (long p=0; p<5; p++) {
std::cout << rr() << "\n"; // keep track of these
}
// repeat for states 2 and 3, i.e., ss2 and ss3
ss1 >> engine;
for (long p=0; p<5; p++) {
std::cout << rr() << "\n"; // compare with the above numbers
}
// repeat for states 2 and 3
我的 Boost 版本是 1.47.0,我正在为 64 位 Windows 编译 VS2010 中的代码。非常感谢任何尝试“重新调整”这些已保存状态的建议。谢谢!