pub struct Rand32 { /* private fields */ }
Expand description
A PRNG producing a 32-bit output.
The current implementation is PCG-XSH-RR
.
Implementations§
Source§impl Rand32
impl Rand32
Sourcepub const DEFAULT_INC: u64 = 1_442_695_040_888_963_407u64
pub const DEFAULT_INC: u64 = 1_442_695_040_888_963_407u64
The default value for increment
.
This is basically arbitrary, it comes from the
PCG reference C implementation:
https://github.com/imneme/pcg-c/blob/master/include/pcg_variants.h#L284
Sourcepub fn new_inc(seed: u64, increment: u64) -> Self
pub fn new_inc(seed: u64, increment: u64) -> Self
Creates a new PRNG. The two inputs, seed
and increment
,
determine what you get; increment
basically selects which
sequence of all those possible the PRNG will produce, and the
seed
selects where in that sequence you start.
Both are arbitrary; increment must be an odd number but this handles that for you
Sourcepub fn state(&self) -> (u64, u64)
pub fn state(&self) -> (u64, u64)
Returns the internal state of the PRNG. This allows you to save a PRNG and create a new one that will resume from the same spot in the sequence.
Sourcepub fn from_state(state: (u64, u64)) -> Self
pub fn from_state(state: (u64, u64)) -> Self
Creates a new PRNG from a saved state from Rand32::state()
.
This is NOT quite the same as new_inc()
because new_inc()
does
a little extra setup work to initialize the state.
Sourcepub fn rand_float(&mut self) -> f32
pub fn rand_float(&mut self) -> f32
Produces a random f32
in the range [0.0, 1.0)
.
Sourcepub fn rand_range(&mut self, range: Range<u32>) -> u32
pub fn rand_range(&mut self, range: Range<u32>) -> u32
Produces a random within the given bounds. Like any Range
,
it includes the lower bound and excludes the upper one.
This should be faster than Self::rand() % end + start
, but the
real advantage is it’s more convenient. Requires that
range.end <= range.start
.