[][src]Struct rand_mt::Mt19937GenRand64

pub struct Mt19937GenRand64 { /* fields omitted */ }

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

Size

Mt19937GenRand64 requires approximately 2.5KB 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

impl Mt19937GenRand64[src]

pub const DEFAULT_SEED: u64[src]

Default seed used by Mt19937GenRand64::new_unseeded.

#[must_use]pub fn new(seed: u64) -> Self[src]

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

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

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

Key can have any length.

#[must_use]pub fn new_unseeded() -> Self[src]

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

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

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

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

Generate next u32 output.

This function is implemented by generating one u64s from the RNG and shifting + masking them into a u32 output.

Examples

let mut mt = Mt19937GenRand64::new_unseeded();
assert_ne!(mt.next_u32(), mt.next_u32());

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

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

pub fn recover<I>(key: I) -> Result<Self, RecoverRngError> where
    I: IntoIterator<Item = u64>, 
[src]

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.

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

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

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

Reseed a Mersenne Twister from am iterator of u64s.

Key can have any length.

Trait Implementations

impl Clone for Mt19937GenRand64[src]

impl Debug for Mt19937GenRand64[src]

impl Default for Mt19937GenRand64[src]

pub fn default() -> Self[src]

Return a new Mt19937GenRand64 with the default seed.

Equivalent to calling Mt19937GenRand64::new_unseeded.

impl Eq for Mt19937GenRand64[src]

impl From<[u64; 312]> for Mt19937GenRand64[src]

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

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.

impl From<[u8; 8]> for Mt19937GenRand64[src]

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

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

impl From<u64> for Mt19937GenRand64[src]

pub fn from(seed: u64) -> Self[src]

Construct a Mersenne Twister RNG from a u64 seed.

This function is equivalent to new.

impl Hash for Mt19937GenRand64[src]

impl Ord for Mt19937GenRand64[src]

impl PartialEq<Mt19937GenRand64> for Mt19937GenRand64[src]

impl PartialOrd<Mt19937GenRand64> for Mt19937GenRand64[src]

impl RngCore for Mt19937GenRand64[src]

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

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

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

Generate next u32 output.

This function is implemented by generating one u64s from the RNG and shifting + masking them 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());

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

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

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

impl SeedableRng for Mt19937GenRand64[src]

type Seed = [u8; 8]

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

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

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

impl StructuralEq for Mt19937GenRand64[src]

impl StructuralPartialEq for Mt19937GenRand64[src]

impl TryFrom<&'_ [u64]> for Mt19937GenRand64[src]

type Error = RecoverRngError

The type returned in the event of a conversion error.

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

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.

Auto Trait Implementations

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