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
impl MersenneTwister
Sourcepub fn new() -> MersenneTwister
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)
.
Sourcepub fn genrand_int32(&mut self) -> u32
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.
Sourcepub fn init_by_array(&mut self, init_key: &[u32])
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.
Sourcepub fn init_genrand(&mut self, s: u32)
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
.
Sourcepub fn genrand_res53(&mut self) -> f64
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).
Sourcepub fn genrand_int31(&mut self) -> i32
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
.
Sourcepub fn genrand_real1(&mut self) -> f64
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).
Sourcepub fn genrand_real2(&mut self) -> f64
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
.
Sourcepub fn genrand_real3(&mut self) -> f64
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).
Sourcepub fn sgenrand(&mut self, seed: u32)
👎Deprecated: Can lead to very poor quality random numbers. Use init_by_array
or init_genrand
instead.
pub fn sgenrand(&mut self, seed: u32)
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
impl Clone for MersenneTwister
Source§fn clone(&self) -> MersenneTwister
fn clone(&self) -> MersenneTwister
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for MersenneTwister
impl RefUnwindSafe for MersenneTwister
impl Send for MersenneTwister
impl Sync for MersenneTwister
impl Unpin for MersenneTwister
impl UnwindSafe for MersenneTwister
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)