An implementation of Ruby’s pseudo-random number generator, or PRNG.
The PRNG produces a deterministic sequence of bits which approximate true randomness. The sequence may be represented by integers, floats, or binary strings.
The generator may be initialized with either a system-generated or user-supplied seed value.
PRNGs are currently implemented as a modified Mersenne Twister with a period of 2**19937-1.
This RNG reproduces the same random bytes and floats as MRI. It may differ when returning elements confined to a distribution.
let seed = [627457_u32, 697550, 16438, 41926]; let mut random = Random::with_array_seed(seed); let rand = random.next_int32();
Generate random numbers in a range:
let mut random = Random::new()?; let max = Max::Integer(10); let mut rand = rand(&mut random, max)?; assert!(matches!(rand, Rand::Integer(x) if x < 10));
This crate is
no_std compatible when built without the
std feature. This
crate does not depend on
All features are enabled by default.
- random-rand - Enables range sampling methods for the
rand()function. Activating this feature also activates the rand-traits feature. Dropping this feature removes the
- rand-traits - Enables implementations of
Randomtype. Dropping this feature removes the
- std - Enables a dependency on the Rust Standard Library. Activating
this feature enables
std::error::Errorimpls on error types in this crate.
Randomrandom number generator failed to initialize.