简体   繁体   中英

How can a mersenne_twister_engine be serialized using Boost?

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

  1. What object can be used to store the state of the mersenne_twister_engine that can also be serialized using Boost?
  2. Is my approach safe and generally considered to be good practice?
  3. Is there an approach to this that is generally considered to be better?

From this output/input operator reference regarding the output operator:

Serializes the internal state of the pseudo-random number engine e as a sequence of decimal numbers separated by one or more spaces, and inserts it to the stream ost .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM