[−][src]Struct spinoso_random::Mt
The 32-bit Ruby flavor of the Mersenne Twister pseudorandom number generator.
The RNG is used to implement the Random
class in Ruby Core. This RNG is
a modified version of the MT19937
random number generator. It natively
outputs u32
.
Mt
is not suitable for cryptographic use.
Size
Mt
requires approximately 2.5KB of internal state.
You may wish to store an Mt
on the heap in a Box
to make
it easier to embed in another struct.
Implementations
impl Mt
[src]
#[must_use]pub fn with_seed(seed: u32) -> Self
[src]
Create a new Mersenne Twister random number generator using the given seed.
Examples
let seed = 123_456_789_u32; let mut mt = Mt::with_seed(seed); let rand = mt.next_int32();
#[must_use]pub fn new_with_key<I>(key: I) -> Self where
I: IntoIterator<Item = u32>,
I::IntoIter: Clone,
[src]
I: IntoIterator<Item = u32>,
I::IntoIter: Clone,
Create a new Mersenne Twister random number generator using the given key.
Key can have any length.
This function is used when initializing an Mt
with more than 32 bits,
such as when initializing with a Bignum
seed.
MRI initializes the global SipHash seed with 4 u32
s of entropy.
#[must_use]pub fn next_int32(&mut self) -> u32
[src]
Generate next u32
output.
Generates a random number on (0..=0xffffffff)
-interval.
u32
is the native output of the generator. This function advances the
RNG step counter by one.
Examples
let mut mt = Mt::with_seed(5489); assert_ne!(mt.next_int32(), mt.next_int32());
#[must_use]pub fn next_real(&mut self) -> f64
[src]
Generate next f64
output.
Generates a random number on [0,1) with 53-bit resolution.
Examples
let mut mt = Mt::with_seed(5489); assert_ne!(mt.next_real(), mt.next_real());
pub fn fill_bytes(&mut self, dest: &mut [u8])
[src]
Fill a buffer with bytes generated from the RNG.
This method generates random u32
s (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 4.
Examples
let mut mt = Mt::with_seed(5489); 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 reseed(&mut self, seed: u32)
[src]
Reseed a Mersenne Twister from a single u32
.
Examples
let mut mt = Mt::with_seed(5489_u32); let first = mt.next_int32(); mt.fill_bytes(&mut [0; 512]); mt.reseed(5489_u32); assert_eq!(first, mt.next_int32());
pub fn reseed_with_key<I>(&mut self, key: I) where
I: IntoIterator<Item = u32>,
I::IntoIter: Clone,
[src]
I: IntoIterator<Item = u32>,
I::IntoIter: Clone,
Reseed a Mersenne Twister from an iterator of u32
s.
Key can have any length.
Trait Implementations
impl Clone for Mt
[src]
impl Debug for Mt
[src]
impl Eq for Mt
[src]
impl Hash for Mt
[src]
pub fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for Mt
[src]
pub fn cmp(&self, other: &Mt) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<Mt> for Mt
[src]
impl PartialOrd<Mt> for Mt
[src]
pub fn partial_cmp(&self, other: &Mt) -> Option<Ordering>
[src]
pub fn lt(&self, other: &Mt) -> bool
[src]
pub fn le(&self, other: &Mt) -> bool
[src]
pub fn gt(&self, other: &Mt) -> bool
[src]
pub fn ge(&self, other: &Mt) -> bool
[src]
impl RngCore for Mt
[src]
#[must_use]pub fn next_u64(&mut self) -> u64
[src]
Generate next u64
output.
This function is implemented by generating two u32
s from the RNG and
shifting + masking them into a u64
output.
Examples
let mut mt = Mt::with_seed(5489); assert_ne!(mt.next_u64(), mt.next_u64());
#[must_use]pub fn next_u32(&mut self) -> u32
[src]
Generate next u32
output.
u32
is the native output of the generator. This function advances the
RNG step counter by one.
Examples
let mut mt = Mt::with_seed(5489); 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 u32
s (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 4.
Examples
let mut mt = Mt::with_seed(5489); 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 try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
[src]
Fill a buffer with bytes generated from the RNG.
This method generates random u32
s (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 4.
try_fill_bytes
is implemented with fill_bytes
and is infallible.
Examples
let mut mt = Mt::with_seed(5489); let mut buf = [0; 32]; mt.try_fill_bytes(&mut buf)?; assert_ne!([0; 32], buf); let mut buf = [0; 31]; mt.try_fill_bytes(&mut buf)?; assert_ne!([0; 31], buf);
Errors
This method never returns an error. It is equivalent to calling the
infallible fill_bytes
method.
impl SeedableRng for Mt
[src]
type Seed = [u8; 16]
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 four u32
s.
Examples
// Default MT seed let seed = 5489_u128.to_le_bytes(); let mut mt = Mt::from_seed(seed); assert_ne!(mt.next_u32(), mt.next_u32());
pub fn seed_from_u64(state: u64) -> Self
[src]
pub fn from_rng<R>(rng: R) -> Result<Self, Error> where
R: RngCore,
[src]
R: RngCore,
pub fn from_entropy() -> Self
[src]
impl StructuralEq for Mt
[src]
impl StructuralPartialEq for Mt
[src]
Auto Trait Implementations
impl RefUnwindSafe for Mt
[src]
impl Send for Mt
[src]
impl Sync for Mt
[src]
impl Unpin for Mt
[src]
impl UnwindSafe for Mt
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<R> Rng for R where
R: RngCore + ?Sized,
[src]
R: RngCore + ?Sized,
pub fn gen<T>(&mut self) -> T where
Standard: Distribution<T>,
[src]
Standard: Distribution<T>,
pub fn gen_range<T, R>(&mut self, range: R) -> T where
T: SampleUniform,
R: SampleRange<T>,
[src]
T: SampleUniform,
R: SampleRange<T>,
pub fn sample<T, D>(&mut self, distr: D) -> T where
D: Distribution<T>,
[src]
D: Distribution<T>,
pub fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T> where
D: Distribution<T>,
[src]
D: Distribution<T>,
pub fn fill<T>(&mut self, dest: &mut T) where
T: Fill + ?Sized,
[src]
T: Fill + ?Sized,
pub fn try_fill<T>(&mut self, dest: &mut T) -> Result<(), Error> where
T: Fill + ?Sized,
[src]
T: Fill + ?Sized,
pub fn gen_bool(&mut self, p: f64) -> bool
[src]
pub fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
[src]
V: MultiLane<T>,