Crate spinoso_random[][src]

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.

Implementation notes

This RNG reproduces the same random bytes and floats as MRI. It may differ when returning elements confined to a distribution.

Examples

Generate integers:

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));

no_std

This crate is no_std compatible when built without the std feature. This crate does not depend on alloc.

Crate features

All features are enabled by default.

Structs

ArgumentError

Error that indicates a random number could not be generated with the given bounds.

InitializeError

Error that indicates a Random random number generator failed to initialize.

Mt

The 32-bit Ruby flavor of the Mersenne Twister pseudorandom number generator.

NewSeedError

Error that indicates the system source of cryptographically secure randomness failed to read sufficient bytes to create a new seed.

Random

Random provides an interface to Ruby’s pseudo-random number generator, or PRNG.

UrandomError

Error that indicates the system source of cryptographically secure randomness failed to read the requested bytes.

Enums

Error

Sum type of all errors possibly returned from Random functions.

Maxrandom-rand

A range constraint for generating random numbers.

Randrandom-rand

A generated random number.

Functions

new_seed

Read a new Random seed, using platform-provided randomness.

randrandom-rand

Generate random numbers bounded from below by 0 and above by the given max.

seed_to_key

Convert a byte array into a reseeding key of u32s.

urandom

Read random bytes, using platform-provided randomness.