Struct spinoso_random::Mt[][src]

pub struct Mt { /* fields omitted */ }

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

impl Mt[src]

#[must_use]pub fn with_seed(seed: u32) -> Self[src]

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

#[must_use]pub fn new_with_key<I>(key: I) -> Self where
    I: IntoIterator<Item = u32>,
    I::IntoIter: Clone
[src]

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.

#[must_use]pub fn next_int32(&mut self) -> u32[src]

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

#[must_use]pub fn next_real(&mut self) -> f64[src]

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

pub fn fill_bytes(&mut self, dest: &mut [u8])[src]

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

pub fn reseed(&mut self, seed: u32)[src]

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

pub fn reseed_with_key<I>(&mut self, key: I) where
    I: IntoIterator<Item = u32>,
    I::IntoIter: Clone
[src]

Reseed a Mersenne Twister from an iterator of u32s.

Key can have any length.

Trait Implementations

impl Clone for Mt[src]

impl Debug for Mt[src]

impl Eq for Mt[src]

impl Hash for Mt[src]

impl Ord for Mt[src]

impl PartialEq<Mt> for Mt[src]

impl PartialOrd<Mt> for Mt[src]

impl RngCore for Mt[src]

#[must_use]fn next_u64(&mut self) -> u64[src]

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

#[must_use]fn next_u32(&mut self) -> u32[src]

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

fn fill_bytes(&mut self, dest: &mut [u8])[src]

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

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>[src]

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.

impl SeedableRng for Mt[src]

type Seed = [u8; 16]

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

fn from_seed(seed: Self::Seed) -> Self[src]

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

impl StructuralEq for Mt[src]

impl StructuralPartialEq for Mt[src]

Auto Trait Implementations

impl RefUnwindSafe for Mt

impl Send for Mt

impl Sync for Mt

impl Unpin for Mt

impl UnwindSafe for Mt

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<R> Rng for R where
    R: RngCore + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]