rand_python

Struct MersenneTwister

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

Implementation of Mersenne Twister (mt19937ar).

This is a direct Rust port of the C implementation known as mt19937ar.c.

It tries to stay as close as possible to the original in terms of function naming and overall interface. This makes it easier to port libraries that use the Mersenne Twister internally.

The code remains functionally the same but has been cleaned up and re-commented to reflect my own understanding of the original code and algorithm. Some comments from the original have been omitted. Errors are mine.

Implementations§

Source§

impl MersenneTwister

Source

pub fn new() -> MersenneTwister

Creates a new, unseeded random number generator.

Seed it using init_by_array, init_genrand, or the deprecated and buggy sgenrand.

Trying to generate random numbers without seeding will cause a panic. The upstream implementation automatically seeds the generator using a hardcoded seed instead. To use that seed, you can manually call init_genrand(5489).

Source

pub fn genrand_int32(&mut self) -> u32

This is the core random number generation function that all the others are based on.

It generates a random u32 value.

Source

pub fn init_by_array(&mut self, init_key: &[u32])

Reset the random number generator with a seed of arbitary length.

The seed is specified as a slice of u32 values. It cannot be specified as an iterator because if the seed is shorter than 624 values each value may be accessed more than once.

Source

pub fn init_genrand(&mut self, s: u32)

Reset the random number generator using a single 32 bit seed.

This is generally not recommended since it means all future output of the random number generator can be reproduced by knowing, guessing, or accidentally picking the same 32 bit seed value.

If you want to seed the random number generator with more than 32 bits of data, see init_by_array.

Source

pub fn genrand_res53(&mut self) -> f64

Generates a random value: f64 such that 0. <= value && value < 1., using 53 bits of randomness (the maximum possible for a f64 value).

Source

pub fn genrand_int31(&mut self) -> i32

Generates a random value: i32 such that 0 <= value && value <= 0x7fffffff.

If you want a u32, see genrand_int32.

Source

pub fn genrand_real1(&mut self) -> f64

Generates a random value: f64 such that 0. <= value && value <= 1., using 32 bits worth of randomness.

If you want value < 1 instead of value <= 1, see genrand_real2 (or genrand_res53 if you also want the maximum amount of randomness).

Source

pub fn genrand_real2(&mut self) -> f64

Generates a random value: f64 such that 0. <= value && value < 1., using 32 bits of randomness.

If you want the maximum amount of randomness, see genrand_res53.

Source

pub fn genrand_real3(&mut self) -> f64

Generates a random value: f64 such that 0. < value && value < 1., using 32 bits of randomness.

If you want 0 <= value instead of 0 < value, see genrand_real2 (or genrand_res53 if you also want the maximum amount of randomness).

Source

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

👎Deprecated: Can lead to very poor quality random numbers. Use init_by_array or init_genrand instead.

Reset the random number generator using a single 32 bit seed.

This old seed function can cause very poor quality random numbers and is mainly included for historical purposes. It is only present in older versions of the Mersenne Twister code base, not the mt19937ar version this module is based on.

Most software uses init_by_array or init_genrand instead, which were introduced in the mt19937ar version.

Trait Implementations§

Source§

impl Clone for MersenneTwister

Source§

fn clone(&self) -> MersenneTwister

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 MersenneTwister

Source§

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

Formats the value using the given formatter. Read more

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

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

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.