Struct MT

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

Contains state of PRNG

Implementations§

Source§

impl MT

Source

pub fn new() -> MT

Create new PRNG seeded with the current timestamp

Source

pub fn from_seed(seed: u64) -> MT

Creates a new generator seeded with the given seed

Source

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

Seed the generator

Newly created MTs are seeded with the current timestamp.

Source

pub fn next(&mut self) -> u64

Interpolate the next integer using MT19937

This method should not be used for generating random numbers within a specific range. For that use any of the methods prefixed with “rand”.

Source

pub fn rand_f32(&mut self) -> f32

Generate an f32 between 0.0 and 1.0

§Example
let mut gen = rustmt::MT::new();
let n = gen.rand_f32();
 
assert!( n >= 0.0 );
assert!( n <= 1.0 );
Source

pub fn rand_f64(&mut self) -> f64

Generate an f64 between 0.0 and 1.0

§Example
let mut gen = rustmt::MT::new();
let n = gen.rand_f64();
 
assert!( n >= 0.0 );
assert!( n <= 1.0 );
Source

pub fn rand_i32(&mut self, start: i32, end: i32) -> i32

Generate an i32 between start-end-1 inclusive

§Example
let mut gen = rustmt::MT::new();
let n = gen.rand_i32( 1, 3 );
 
assert!( n >= 1 );
assert!( n < 3 );
§Panics

When start is greater than or equal to end

Source

pub fn rand_i64(&mut self, start: i64, end: i64) -> i64

Generate an i64 between start-end-1 inclusive

§Example
let mut gen = rustmt::MT::new();
let n = gen.rand_i64( 1, 3 );
 
assert!( n >= 1 );
assert!( n < 3 );
§Panics

When start is greater than or equal to end

Source

pub fn rand_usize(&mut self, end: usize) -> usize

Generate a usize less than end

§Example
let mut gen = rustmt::MT::new();
 
let v = vec![ 1, 2, 3, ];
let n = gen.rand_usize( v.len() );
 
assert!( n < v.len() );
Source

pub fn shuffle<T>(&mut self, v: &mut Vec<T>)

Shuffle the given vector

§Example
let mut gen = rustmt::MT::new();
 
let mut people = vec![ "Logan", "Trent", "Cameron", "David", ];
gen.shuffle( &mut people );
 
println!( "Random person = {}", people[0] );

Auto Trait Implementations§

§

impl Freeze for MT

§

impl RefUnwindSafe for MT

§

impl Send for MT

§

impl Sync for MT

§

impl Unpin for MT

§

impl UnwindSafe for MT

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