How to save C ++ 0X state random number generator

I play with the new free C ++ 0X library and based on this question:
What is the standard way to get the state of a C ++ 0x random number generator?
it seems that if you do not know the seed for the current state of a random generator, the only way to save its state is to save the generator in a stream. For this, I wrote the following

#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; } 

Displays

Comparison of engines: 1
Engine 1 random number 581869302
Engine 2 random number 4178893912

I have a few questions:

  • Why are the following numbers from engine1 and engine2 different?
  • Why are two engines compared even if their next numbers are different?
  • What am I doing wrong in my example and how to save the state of a random engine in order to get repeatability in subsequent runs (if you donโ€™t know the seed to set the desired state)?

Thanks.

+4
source share
1 answer

It looks like a mistake to me. I ran your libC ++ code , and the output is:

 Engine comparison: 1 Engine 1 random number 581869302 Engine 2 random number 581869302 
+3
source

All Articles