I have a class that implements std::mt19937 and I would like to be able to save and load the state of the random number generator for consistency. I'm aware that the << and >> operators can be used to save/load the engine's state and that I can separate the save and load functions when I archive the file if necessary. My guess at how to do this would be to use the << and >> operators to store the state into/extract the state from an object that can be serialized. My questions are
- What object can be used to store the state of the mersenne_twister_engine that can also be serialized using Boost?
- Is my approach safe and generally considered to be good practice?
- Is there an approach to this that is generally considered to be better?
From this output/input operator reference regarding the output operator:
The stream could be any kind of output stream, for example an output string stream. You can then use the string that the string-streams created and pass it on to whatever serialization framework you need.
Or if the serialization framework support direct output/input stream operations, you can use it directly with the engine operators.