pub struct Prng { /* private fields */ }Expand description
A small, deterministic pseudo-random generator that lives inside game state.
The whole reproducible position is a single u64 (Prng::position), so
the generator is Copy, serializes with the state it lives in (O(1)
snapshot and resume), and a Reversible game’s undo record can restore its
position exactly. Everything is integer-only with a fixed algorithm, so a
seed reproduces the same stream on every platform. Do not rely on the
concrete algorithm (currently PCG XSH-RR 64/32, single stream): it is an
implementation detail of this newtype and may change.
This is not cryptographically secure and must not be used for security.
Implementations§
Source§impl Prng
impl Prng
Sourcepub const fn new(seed: u64) -> Self
pub const fn new(seed: u64) -> Self
Creates a generator seeded from seed.
Equal seeds produce identical streams; different seeds are extremely likely to diverge immediately.
Sourcepub const fn position(&self) -> u64
pub const fn position(&self) -> u64
Returns the generator’s current position in its stream.
Capture this before a move and pass it to Prng::set_position to
rewind, which is how a make/unmake (Reversible) undo record restores
the random stream.
Sourcepub const fn set_position(&mut self, position: u64)
pub const fn set_position(&mut self, position: u64)
Restores a position previously read from Prng::position.
Sourcepub const fn next_u32(&mut self) -> u32
pub const fn next_u32(&mut self) -> u32
Returns the next 32-bit value and advances the stream by one step.
Sourcepub const fn next_u64(&mut self) -> u64
pub const fn next_u64(&mut self) -> u64
Returns the next 64-bit value (two steps of the stream).
Sourcepub const fn below(&mut self, bound: u64) -> u64
pub const fn below(&mut self, bound: u64) -> u64
Returns a uniformly distributed value in 0..bound.
Uses rejection sampling to avoid modulo bias, so a single call consumes a
variable number of steps. That is why a pre-move Prng::position must
be snapshotted rather than reconstructed by counting draws.
§Panics
Panics if bound is zero.