Struct spinoso_random::Mt[][src]

pub struct Mt { /* fields omitted */ }
Expand description

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

The RNG is used to implement the Random class in Ruby Core. This RNG is a modified version of the MT19937 random number generator. It natively outputs u32.

Mt is not suitable for cryptographic use.

Size

Mt requires approximately 2.5KB of internal state.

You may wish to store an Mt on the heap in a Box to make it easier to embed in another struct.

Implementations

Create a new Mersenne Twister random number generator using the given seed.

Examples

let seed = 123_456_789_u32;
let mut mt = Mt::with_seed(seed);
let rand = mt.next_int32();

Create a new Mersenne Twister random number generator using the given key.

Key can have any length.

This function is used when initializing an Mt with more than 32 bits, such as when initializing with a Bignum seed.

MRI initializes the global SipHash seed with 4 u32s of entropy.

Generate next u32 output.

Generates a random number on (0..=0xffffffff)-interval.

u32 is the native output of the generator. This function advances the RNG step counter by one.

Examples

let mut mt = Mt::with_seed(5489);
assert_ne!(mt.next_int32(), mt.next_int32());

Generate next f64 output.

Generates a random number on [0,1) with 53-bit resolution.

Examples

let mut mt = Mt::with_seed(5489);
assert_ne!(mt.next_real(), mt.next_real());

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

Examples

let mut mt = Mt::with_seed(5489);
let mut buf = [0; 32];
mt.fill_bytes(&mut buf);
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
mt.fill_bytes(&mut buf);
assert_ne!([0; 31], buf);

Reseed a Mersenne Twister from a single u32.

Examples

let mut mt = Mt::with_seed(5489_u32);
let first = mt.next_int32();
mt.fill_bytes(&mut [0; 512]);
mt.reseed(5489_u32);
assert_eq!(first, mt.next_int32());

Reseed a Mersenne Twister from an iterator of u32s.

Key can have any length.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Generate next u64 output.

This function is implemented by generating two u32s from the RNG and shifting + masking them into a u64 output.

Examples

let mut mt = Mt::with_seed(5489);
assert_ne!(mt.next_u64(), mt.next_u64());

Generate next u32 output.

u32 is the native output of the generator. This function advances the RNG step counter by one.

Examples

let mut mt = Mt::with_seed(5489);
assert_ne!(mt.next_u32(), mt.next_u32());

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

Examples

let mut mt = Mt::with_seed(5489);
let mut buf = [0; 32];
mt.fill_bytes(&mut buf);
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
mt.fill_bytes(&mut buf);
assert_ne!([0; 31], buf);

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

try_fill_bytes is implemented with fill_bytes and is infallible.

Examples

let mut mt = Mt::with_seed(5489);
let mut buf = [0; 32];
mt.try_fill_bytes(&mut buf)?;
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
mt.try_fill_bytes(&mut buf)?;
assert_ne!([0; 31], buf);

Errors

This method never returns an error. It is equivalent to calling the infallible fill_bytes method.

Reseed from four u32s.

Examples

// Default MT seed
let seed = 5489_u128.to_le_bytes();
let mut mt = Mt::from_seed(seed);
assert_ne!(mt.next_u32(), mt.next_u32());

Seed type, which is restricted to types mutably-dereferencable as u8 arrays (we recommend [u8; N] for some N). Read more

Create a new PRNG using a u64 seed. Read more

Create a new PRNG seeded from another Rng. Read more

Creates a new instance of the RNG seeded via getrandom. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Return a random value supporting the Standard distribution. Read more

Generate a random value in the given range. Read more

Sample a new value, using the given distribution. Read more

Create an iterator that generates values using the given distribution. Read more

Fill any type implementing Fill with random data Read more

Fill any type implementing Fill with random data Read more

Return a bool with a probability p of being true. Read more

Return a bool with a probability of numerator/denominator of being true. I.e. gen_ratio(2, 3) has chance of 2 in 3, or about 67%, of returning true. If numerator == denominator, then the returned value is guaranteed to be true. If numerator == 0, then the returned value is guaranteed to be false. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.