Struct rand_mt::Mt19937GenRand64

source ·
pub struct Mt19937GenRand64 { /* private fields */ }
Expand description

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

§Size

Mt19937GenRand64 requires approximately 2.5 kilobytes of internal state.

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

Mt19937GenRand64 is also the same size as Mt19937GenRand32.

assert_eq!(2504, mem::size_of::<Mt19937GenRand64>());
assert_eq!(mem::size_of::<Mt19937GenRand32>(), mem::size_of::<Mt19937GenRand64>());

Implementations§

source§

impl Mt19937GenRand64

source

pub const DEFAULT_SEED: u64 = 5_489u64

Default seed used by Mt19937GenRand64::new_unseeded.

source

pub fn new(seed: u64) -> Self

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

§Examples
§Constructing with a u64 seed
let seed = 123_456_789_u64;
let mt1 = Mt19937GenRand64::new(seed);
let mt2 = Mt19937GenRand64::from(seed.to_le_bytes());
assert_eq!(mt1, mt2);
§Constructing with default seed
let mt1 = Mt19937GenRand64::new(Mt19937GenRand64::DEFAULT_SEED);
let mt2 = Mt19937GenRand64::new_unseeded();
assert_eq!(mt1, mt2);
source

pub fn new_with_key<I>(key: I) -> Self
where I: IntoIterator<Item = u64>, 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_u64;
let mt = Mt19937GenRand64::new(seed);
let unseeded = Mt19937GenRand64::new_unseeded();
assert_eq!(mt, unseeded);
source

pub fn next_u64(&mut self) -> u64

Generate next u64 output.

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

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

pub fn next_u32(&mut self) -> u32

Generate next u32 output.

This function is implemented by generating one u64 from the RNG and performing shifting and masking to turn it into a u32 output.

§Examples
let mut mt = Mt19937GenRand64::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 u64s (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 8.

§Examples
let mut mt = Mt19937GenRand64::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 = u64>,

Attempt to recover the internal state of a Mersenne Twister using the past 312 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 312 elements, an error is returned because there is not enough data to fully initialize the RNG.

If key has more than 312 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: u64)

Reseed a Mersenne Twister from a single u64.

§Examples
// Default MT seed
let mut mt = Mt19937GenRand64::new_unseeded();
let first = mt.next_u64();
mt.fill_bytes(&mut [0; 512]);
// Default MT seed
mt.reseed(5489_u64);
assert_eq!(first, mt.next_u64());
source

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

Reseed a Mersenne Twister from am iterator of u64s.

Key can have any length.

Trait Implementations§

source§

impl Clone for Mt19937GenRand64

source§

fn clone(&self) -> Mt19937GenRand64

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 Mt19937GenRand64

source§

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

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

impl Default for Mt19937GenRand64

source§

fn default() -> Self

Return a new Mt19937GenRand64 with the default seed.

Equivalent to calling Mt19937GenRand64::new_unseeded.

source§

impl From<[u64; 312]> for Mt19937GenRand64

source§

fn from(key: [u64; 312]) -> Self

Recover the internal state of a Mersenne Twister using the past 312 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; 8]> for Mt19937GenRand64

source§

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

Construct a Mersenne Twister RNG from 8 bytes.

§Examples
// Default MT seed
let seed = 5489_u64.to_le_bytes();
let mut mt = Mt19937GenRand64::from(seed);
assert_ne!(mt.next_u64(), mt.next_u64());
source§

impl From<u64> for Mt19937GenRand64

source§

fn from(seed: u64) -> Self

Construct a Mersenne Twister RNG from a u64 seed.

This function is equivalent to new.

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

// Non-default MT seed
let seed = 9927_u64;
let mt1 = Mt19937GenRand64::from(seed);
let mt2 = Mt19937GenRand64::new(seed);
assert_eq!(mt1, mt2);
source§

impl Hash for Mt19937GenRand64

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 Mt19937GenRand64

source§

fn cmp(&self, other: &Mt19937GenRand64) -> 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 + PartialOrd,

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

impl PartialEq for Mt19937GenRand64

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Mt19937GenRand64

source§

fn partial_cmp(&self, other: &Mt19937GenRand64) -> 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

This method 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

This method 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

This method 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

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

impl RngCore for Mt19937GenRand64

source§

fn next_u64(&mut self) -> u64

Generate next u64 output.

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

§Examples
use rand_core::RngCore;
use rand_mt::Mt19937GenRand64;

let mut rng = Mt19937GenRand64::new_unseeded();
assert_ne!(rng.next_u64(), rng.next_u64());
source§

fn next_u32(&mut self) -> u32

Generate next u32 output.

This function is implemented by generating one u64 from the RNG and performing shifting and masking to turn it into a u32 output.

§Examples
use rand_core::RngCore;
use rand_mt::Mt19937GenRand64;

let mut rng = Mt19937GenRand64::new_unseeded();
assert_ne!(rng.next_u32(), rng.next_u32());
source§

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

Fill a buffer with bytes generated from the RNG.

This method generates random u64s (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 8.

§Examples
use rand_core::RngCore;
use rand_mt::Mt19937GenRand64;

let mut rng = Mt19937GenRand64::new_unseeded();
let mut buf = [0; 32];
rng.fill_bytes(&mut buf);
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
rng.fill_bytes(&mut buf);
assert_ne!([0; 31], buf);
source§

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

Fill a buffer with bytes generated from the RNG.

This method generates random u64s (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 8.

try_fill_bytes is implemented with fill_bytes and is infallible.

§Examples
use rand_core::RngCore;
use rand_mt::Mt19937GenRand64;

let mut rng = Mt19937GenRand64::new_unseeded();
let mut buf = [0; 32];
rng.try_fill_bytes(&mut buf)?;
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
rng.try_fill_bytes(&mut buf)?;
assert_ne!([0; 31], buf);
source§

impl SeedableRng for Mt19937GenRand64

source§

fn from_seed(seed: Self::Seed) -> Self

Reseed from a little endian encoded u64.

§Examples
// Default MT seed
let seed = 5489_u64.to_le_bytes();
let mut mt = Mt19937GenRand64::from_seed(seed);
assert_ne!(mt.next_u64(), mt.next_u64());
§

type Seed = [u8; 8]

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

fn seed_from_u64(state: u64) -> Self

Create a new PRNG using a u64 seed. Read more
source§

fn from_rng<R>(rng: R) -> Result<Self, Error>
where R: RngCore,

Create a new PRNG seeded from another Rng. Read more
source§

impl TryFrom<&[u64]> for Mt19937GenRand64

source§

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

Attempt to recover the internal state of a Mersenne Twister using the past 312 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 Mt19937GenRand64::recover.

§Errors

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

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

§

type Error = RecoverRngError

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

impl Eq for Mt19937GenRand64

source§

impl StructuralPartialEq for Mt19937GenRand64

Auto Trait Implementations§

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> 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

§

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>,

§

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.