我正在使用新的C ++ 0X随机库并基于这个问题:
What is the standard way to get the state of a C++0x random number generator?
似乎如果你不知道随机生成器当前状态的种子,保存其状态的唯一方法是将生成器存储在流中。为此,我写了以下
#include <iostream>
#include <sstream>
#include <random>
int main(int /*argc*/, char** /*argv*/)
{
std::mt19937 engine1;
unsigned int var = engine1(); // Just to get engine1 out of its initial state
std::stringstream input;
input << engine1;
std::mt19937 engine2;
input >> engine2;
std::cout<<"Engine comparison: "<<(engine1 == engine2)<<std::endl;
std::cout<<"Engine 1 random number "<<engine1()<<std::endl;
std::cout<<"Engine 2 random number "<<engine2()<<std::endl;
}
此输出
发动机比较:1
发动机1随机号581869302
发动机2随机数4178893912
我有几个问题:
谢谢。
答案 0 :(得分:3)
对我来说这看起来像个错误。我在libc++上运行了您的代码,输出为:
Engine comparison: 1
Engine 1 random number 581869302
Engine 2 random number 581869302