Saving and restoring the state of a SeedableRng

I'm running simulations that make use of SeedableRng. At the start of a simulation I set the seed, so that I can get reproducible results if I need to re-run the simulation. That's all great, and works well.

I'm wondering if I can save the state of my simulation mid-way through, and later restore it, to continue as if I never interrupted it.

For that, I think I need to somehow save the sate of the random number generator, and then restore it. Is that something that exists? (I don't need crypto-strength randomness, BTW).

An idea situation would allow this: if my simulation takes 10 steps while using a random number generator, if I interrupted the simulation after 6 steps I could (somehow) save all the random generator state (and much other application state, of course), and later restore the seed state and resume the run. The end results (after 10 steps) would be the same as if I had never interrupted the simulation.

Many thanks
Richard

The rand_pcg crate, if its optional "serde" feature is enabled, has SeedableRng types that can be serialized to (and deserialized from) any format with serde support.

2 Likes

Thank you so much! That works just fine for me.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.