pub struct SeededRng { /* private fields */ }Expand description
A seeded xoshiro256++ generator with a standard normal sampler.
It serializes as its 256-bit state, written as four hexadecimal strings ("0x…", because JSON
readers such as JavaScript’s lose integers above 2⁵³), and the spare normal deviate, so a run
can be checkpointed and resumed bit for bit. The all-zero state is rejected: xoshiro would stay
at zero forever.
Implementations§
Source§impl SeededRng
impl SeededRng
Sourcepub fn seed_from_u64(seed: u64) -> Self
pub fn seed_from_u64(seed: u64) -> Self
Seeds the generator: the four state words are the first four SplitMix64 outputs starting
from seed. SplitMix64 never gives four zero words in a row, so every seed is valid.
Sourcepub fn for_stream(seed: u64, keys: &[u64]) -> Self
pub fn for_stream(seed: u64, keys: &[u64]) -> Self
A generator for one stream of a seeded run, keyed by keys: a Monte Carlo sample’s index
and the variable it draws, say. The stream depends only on seed and keys, so a sample
draws the same numbers whatever else the run draws, however many samples it has and
however they are shared among threads: the counter-based idea of J. K. Salmon, M. A.
Moraes, R. O. Dror and D. E. Shaw, “Parallel random numbers: as easy as 1, 2, 3”, Proc.
SC11 (2011), https://doi.org/10.1145/2063384.2063405.
The key folds into one word with SplitMix64’s output function mix, a bijection on 64-bit
words: h₀ = seed, hᵢ = mix(hᵢ₋₁ ⊕ mix(kᵢ + γ)) with γ SplitMix64’s increment, and the
generator is SeededRng::seed_from_u64(hₙ). Two different keys give the same hₙ only
by a collision of 64-bit hashes, about one chance in 2⁶⁴ for a pair of streams; with no
keys the stream is seed_from_u64(seed)’s.
Sourcepub fn next_u64(&mut self) -> u64
pub fn next_u64(&mut self) -> u64
The next 64 random bits (the xoshiro256++ output function and state transition).
Sourcepub fn uniform(&mut self) -> f64
pub fn uniform(&mut self) -> f64
A uniform deviate on [0, 1): the top 53 bits of SeededRng::next_u64 times 2⁻⁵³,
so every value is a multiple of 2⁻⁵³.
Sourcepub fn standard_normal(&mut self) -> f64
pub fn standard_normal(&mut self) -> f64
A standard normal deviate (mean 0, variance 1) by the Marsaglia–Bray polar method.
Each accepted pair gives two deviates; the second is returned by the next call. On average
a pair costs 4/π ≈ 1.27 attempts, two uniforms each.