[rand_pcg] Add internal state fetching and construction#108
[rand_pcg] Add internal state fetching and construction#108dhardy merged 2 commits intorust-random:masterfrom
Conversation
|
I don't have any problem exposing the state, but would prefer the following approach: fn state(&self) -> u128
fn stream(&self) -> u128This API is more flexible and can be used with If you agree to this API, please do the same for all |
|
The reason I need this functionality is for reproducible simulations. I have an MCMC with Pcg64 as the source of randomness, and I need to be able to dump it to a checkpoint file and restore an identical generator later. But if parameters go through This is already possible with serde, but I use a different serialization library. So I thought it'd be nice to have a way to do the same thing without serde here |
|
Sorry, I was forgetting that /// Construct an instance using a pre-initialized `state`
///
/// Unlike [`Self::new`], this method does not mutate `state`. It may
/// therefore be used with [`Self::state`] to reconstruct an RNG.
///
/// Note that the highest bit of the `stream` parameter is discarded
/// to simplify upholding internal invariants.
pub fn from_state(state: u128, stream: u128) -> Self {
// The increment must be odd, hence we discard one bit:
let increment = (stream << 1) | 1;
Lcg128Xsl64 { state, increment }
} |
969d0d2 to
82ccd49
Compare
to_state and from_state to Lcg128Xsl64
|
dhardy
left a comment
There was a problem hiding this comment.
LGTM; just needs the changelog title fixing for the release.
This PR adds methods for retrieving inner state from a
Lcg128Xsl64instance and initializing it from that inner state.I have a use case very similar to #64, where I want to manually serialize and deserialize an RNG object without going through serde. PCG is also not cryptographic, so retrieving its state shouldn't be problematic.
from_stateis almost the same asnew, butnewdoesn't allow passingincrementverbatim because it sets the lowest bit so thatincrementis odd (and it has to do that to match the original implementation).This is only implemented for Pcg64. I can implement it for other variants if that's a problem