-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Random number generators not serializable #84
Comments
Seconding this. For a lot of applications (well, this is a common requirement for games, at least) that require a seedable RNG, you want to be able to save and restore the state as well, as simply reseeding with the same seed used initially won't pick up where the RNG left off (obviously). |
I need this as well. The implementation should be no problem. If you look at eg. XorShiftRng, it's just a 128-bit plain-old-data blob. I'm currently working around this with my own type that just wraps XorShiftRng and then uses unsafe code to cast the XorShift instance into a binary blob and back for serialization. |
Maybe the simplest way to go would be adding a way to extract the seed from pub trait SeedableRng<Seed>: Rng {
fn reseed(&mut self, Seed);
fn from_seed(seed: Seed) -> Self;
fn extract_seed(&self) -> Seed; //new
} This way, you can resume simply with let rng = SeedableRng::from_seed(old_rng.extract_seed()) I don't know, if this would work for every implementation of this trait out there. And it is obviously a none backward compatible change. |
Thanks to @LinearZoetrope serialisation is now available for the ISAAC and Xorshift generators. This will be part of the 0.5 release, although that isn't ready yet. Back-porting to 0.4 should be possible, if someone wants it sooner. Cryptographic generators are not serialisable due to a difference of opinion on whether they should leak state (undecided). |
Regarding cryptographic generators, see this issue: dhardy#31 |
@dhardy If possible, backporting would be nice. I was running with waiting until 0.5 for a while, but some of my users are having problems with my |
0.5 is going to a bit longer yet, so backporting sounds sensible. I don't think there are any major issues (although the PRNG code got moved around a bit). Will you open a PR against the 0.4 branch? |
It would be nice for the
rand
number generators to implementRustcEncodable
andRustcDecodable
.While I am not familiar with the possible implementation difficulties, serializability of random number generators is quite useful in Python's
numpy
for instance --- where the Mersenne twister implementation is easily serializable given that its state is just a regularnumpy
array.The text was updated successfully, but these errors were encountered: