Skip to main content

MersenneTwister

Struct MersenneTwister 

Source
pub struct MersenneTwister {
    pub mt: [u32; 624],
    pub mti: usize,
}
Expand description

Canonical MT19937 generator (N = 624, M = 397).

2496-byte state - kept behind alloc because RngBackend always boxes it.

§Examples

use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.seed(42);
let n = mt.rand();

Fields§

§mt: [u32; 624]

Internal MT19937 state vector - 624 32-bit words.

§Examples

use vrd::random::MersenneTwister;

let mt = MersenneTwister::new();
assert_eq!(mt.mt.len(), 624);
§mti: usize

Current index into Self::mt. Reaches 624 and triggers the next twist; an unseeded generator starts at 625.

§Examples

use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
assert_eq!(mt.mti, 625); // unseeded sentinel
mt.seed(1);
assert_eq!(mt.mti, 624);

Implementations§

Source§

impl MersenneTwister

Source

pub fn new() -> Self

Creates a new generator with an empty state. Call Self::seed before use.

§Examples
use vrd::random::MersenneTwister;

let mt = MersenneTwister::new();
assert_eq!(mt.mti(), 625);
Source

pub fn from_seed(seed: [u8; 32]) -> Self

Seeds the generator from a 32-byte buffer; only the low 4 bytes are needed for MT19937 itself, but accepting 32 bytes keeps the API consistent with Xoshiro256PlusPlus::from_seed. The full 32 bytes are mixed via XOR-fold so callers don’t silently lose entropy.

§Examples
use vrd::random::MersenneTwister;

let seed = [1u8; 32];
let mt = MersenneTwister::from_seed(seed);
Source

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

Seeds the MT state from a 32-bit value using the canonical Knuth multiplier constant.

§Examples
use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.seed(12345);
Source

pub fn twist(&mut self)

Performs the MT twist on the state vector.

§Examples
use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.seed(42);
mt.twist();
Source

pub fn rand(&mut self) -> u32

Generates the next u32.

§Examples
use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.seed(42);
let n = mt.rand();
Source

pub fn next_u64(&mut self) -> u64

Generates the next u64 by combining two u32 outputs.

§Examples
use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.seed(42);
let n = mt.next_u64();
Source

pub fn mti(&self) -> usize

Returns the current index into the state vector.

§Examples
use vrd::random::MersenneTwister;

let mt = MersenneTwister::new();
assert_eq!(mt.mti(), 625);
Source

pub fn set_mti(&mut self, value: usize)

Forces the index to value; mostly useful for round-trip tests.

§Examples
use vrd::random::MersenneTwister;

let mut mt = MersenneTwister::new();
mt.set_mti(100);
assert_eq!(mt.mti(), 100);

Trait Implementations§

Source§

impl Clone for MersenneTwister

Source§

fn clone(&self) -> MersenneTwister

Returns a duplicate 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 MersenneTwister

Source§

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

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

impl Default for MersenneTwister

Source§

fn default() -> Self

Returns a new instance using Self::new.

Source§

impl<'de> Deserialize<'de> for MersenneTwister

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for MersenneTwister

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 MersenneTwister

Source§

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

Source§

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

Source§

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

Source§

type Seed = [u8; 32]

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

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

Create a new PRNG using the given seed. Read more
§

fn seed_from_u64(state: u64) -> Self

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

fn from_rng<R>(rng: &mut R) -> Self
where R: Rng + ?Sized,

Create a new PRNG seeded from an infallible Rng. Read more
§

fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRng>::Error>
where R: TryRng + ?Sized,

Create a new PRNG seeded from a potentially fallible Rng. Read more
§

fn fork(&mut self) -> Self
where Self: Rng,

Fork this PRNG Read more
§

fn try_fork(&mut self) -> Result<Self, Self::Error>
where Self: TryRng,

Fork this PRNG Read more
Source§

impl Serialize for MersenneTwister

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryRng for MersenneTwister

Source§

type Error = Infallible

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, Self::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, Self::Error>

Return the next random u64.
Source§

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

Fill dst entirely with random data.
Source§

impl Eq for MersenneTwister

Source§

impl StructuralPartialEq for MersenneTwister

Auto Trait Implementations§

Blanket Implementations§

§

impl<R> TryRngCore for R
where R: TryRng,

§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0: use TryRng instead
Error type.
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.

§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

§

fn next_u32(&mut self) -> u32

Return the next random u32.
§

fn next_u64(&mut self) -> u64

Return the next random u64.
§

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

Fill dest with random data. Read more
§

impl<R> RngExt for R
where R: Rng + ?Sized,

§

fn random<T>(&mut self) -> T
where StandardUniform: Distribution<T>,

Return a random value via the StandardUniform distribution. Read more
§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
where Self: Sized, StandardUniform: Distribution<T>,

Return an iterator over random variates Read more
§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
§

fn random_bool(&mut self, p: f64) -> bool

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

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

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

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

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

fn fill<T>(&mut self, dest: &mut [T])
where T: Fill,

Fill any type implementing [Fill] with random data Read more
Source§

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

Source§

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

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<R> RngCore for R
where R: Rng,