I want to be able to save and load the state of an RNG so I can reproduce the same random values from a given point (application save/snapshot).
I see there is an operator << and >> overload, which seems to save to a string as a sequence of numbers.
Yes, operator<<
and operator>>
are the only way to import or export a random number generator's state. You can easily convert the textual representation to and from binary, if you'd like.
De-serializing and serializing mt19937
state should be portable between implementations. The result of reading and writing the engine's state via the streaming operators is well defined by the standard, as long as you ensure the streams are imbued with the same locale.
See § 26.5.1.5 for the requirements of operator<<
and operator>>
, followed by § 26.5.3.2 for the textual representation of mersenne_twister_engine
, which mt19937
is a well defined typedef of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With