Struct Mt

Source
pub struct Mt { /* private fields */ }
Expand description

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

The official name of this RNG is MT19937. It natively outputs u32.

§Size

Mt requires approximately 2.5 kilobytes 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.

Mt is also the same size as Mt64.

assert_eq!(2504, size_of::<Mt>());
assert_eq!(size_of::<Mt64>(), size_of::<Mt>());

Implementations§

Source§

impl Mt

Source

pub const DEFAULT_SEED: u32 = 5_489u32

Default seed used by Mt::new_unseeded.

Source

pub fn new(seed: u32) -> Self

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

§Examples
§Constructing with a u32 seed
let seed = 123_456_789_u32;
let mt1 = Mt::new(seed);
let mt2 = Mt::from(seed.to_le_bytes());
assert_eq!(mt1, mt2);
§Constructing with default seed
let mt1 = Mt::new(Mt::DEFAULT_SEED);
let mt2 = Mt::new_unseeded();
assert_eq!(mt1, mt2);
Source

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

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

Key can have any length.

Source

pub fn new_unseeded() -> Self

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

§Examples
// Default MT seed
let seed = 5489_u32;
let mt = Mt::new(seed);
let unseeded = Mt::new_unseeded();
assert_eq!(mt, unseeded);
Source

pub fn next_u64(&mut self) -> u64

Generate next u64 output.

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

§Examples
let mut mt = Mt::new_unseeded();
assert_ne!(mt.next_u64(), mt.next_u64());
Source

pub fn next_u32(&mut self) -> u32

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::new_unseeded();
assert_ne!(mt.next_u32(), mt.next_u32());
Source

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

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::new_unseeded();
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);
Source

pub fn recover<I>(key: I) -> Result<Self, RecoverRngError>
where I: IntoIterator<Item = u32>,

Attempt to recover the internal state of a Mersenne Twister using the past 624 samples.

This conversion takes a history of samples from a RNG and returns a RNG that will produce identical output to the RNG that supplied the samples.

This constructor is also available as a TryFrom implementation for &[u32].

§Errors

If key has less than 624 elements, an error is returned because there is not enough data to fully initialize the RNG.

If key has more than 624 elements, an error is returned because the recovered RNG will not produce identical output to the RNG that supplied the samples.

Source

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

Reseed a Mersenne Twister from a single u32.

§Examples
// Default MT seed
let mut mt = Mt::new_unseeded();
let first = mt.next_u32();
mt.fill_bytes(&mut [0; 512]);
// Default MT seed
mt.reseed(5489_u32);
assert_eq!(first, mt.next_u32());
Source

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

Reseed a Mersenne Twister from am iterator of u32s.

Key can have any length.

Trait Implementations§

Source§

impl Clone for Mt

Source§

fn clone(&self) -> Mt

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Mt

Source§

fn default() -> Self

Return a new Mt with the default seed.

Equivalent to calling Mt::new_unseeded.

Source§

impl From<[u32; 624]> for Mt

Source§

fn from(key: [u32; 624]) -> Self

Recover the internal state of a Mersenne Twister using the past 624 samples.

This conversion takes a history of samples from a RNG and returns a RNG that will produce identical output to the RNG that supplied the samples.

Source§

impl From<[u8; 4]> for Mt

Source§

fn from(seed: [u8; 4]) -> Self

Construct a Mersenne Twister RNG from 4 bytes.

The given bytes are treated as a little endian encoded u32.

§Examples
// Default MT seed
let seed = 5489_u32.to_le_bytes();
let mut mt = Mt::from(seed);
assert_ne!(mt.next_u32(), mt.next_u32());

This constructor is equivalent to passing a little endian encoded u32.

// Default MT seed
let seed = 5489_u32.to_le_bytes();
let mt1 = Mt::from(seed);
let mt2 = Mt::new(5489_u32);
assert_eq!(mt1, mt2);
Source§

impl From<u32> for Mt

Source§

fn from(seed: u32) -> Self

Construct a Mersenne Twister RNG from a u32 seed.

This function is equivalent to new.

§Examples
// Default MT seed
let seed = 5489_u32;
let mt1 = Mt::from(seed);
let mt2 = Mt::new(seed);
assert_eq!(mt1, mt2);

// Non-default MT seed
let seed = 9927_u32;
let mt1 = Mt::from(seed);
let mt2 = Mt::new(seed);
assert_eq!(mt1, mt2);
Source§

impl Hash for Mt

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Ord for Mt

Source§

fn cmp(&self, other: &Mt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Mt

Source§

fn eq(&self, other: &Mt) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Mt

Source§

fn partial_cmp(&self, other: &Mt) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&[u32]> for Mt

Source§

fn try_from(key: &[u32]) -> Result<Self, Self::Error>

Attempt to recover the internal state of a Mersenne Twister using the past 624 samples.

This conversion takes a history of samples from a RNG and returns a RNG that will produce identical output to the RNG that supplied the samples.

This conversion is implemented with Mt::recover.

§Errors

If key has less than 624 elements, an error is returned because there is not enough data to fully initialize the RNG.

If key has more than 624 elements, an error is returned because the recovered RNG will not produce identical output to the RNG that supplied the samples.

Source§

type Error = RecoverRngError

The type returned in the event of a conversion error.
Source§

impl Eq for Mt

Source§

impl StructuralPartialEq for Mt

Auto Trait Implementations§

§

impl Freeze for Mt

§

impl RefUnwindSafe for Mt

§

impl Send for Mt

§

impl Sync for Mt

§

impl Unpin for Mt

§

impl UnwindSafe for Mt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.