Struct rug::rand::RandState
[−]
[src]
pub struct RandState<'a> { /* fields omitted */ }
The state of a random number generator.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let u = rand.bits(32); println!("32 random bits: {:032b}", u);
Methods
impl<'a> RandState<'a>
[src]
fn new() -> RandState<'a>
[src]
Creates a new random generator with a compromise between speed and randomness.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let u = rand.bits(32); println!("32 random bits: {:032b}", u);
fn new_mersenne_twister() -> RandState<'a>
[src]
Creates a random generator with a Mersenne Twister algorithm.
Examples
use rug::rand::RandState; let mut rand = RandState::new_mersenne_twister(); let u = rand.bits(32); println!("32 random bits: {:032b}", u);
fn new_linear_congruential(a: &Integer, c: u32, bits: u32) -> RandState<'a>
[src]
Creates a new random generator with a linear congruential algorithm X = (a × X + c) mod 2bits.
Examples
use rug::Integer; use rug::rand::RandState; let a = match Integer::from_str_radix("292787ebd3329ad7e7575e2fd", 16) { Ok(i) => i, Err(_) => unreachable!(), }; let c = 1; let bits = 100; let mut rand = RandState::new_linear_congruential(&a, c, bits); let u = rand.bits(32); println!("32 random bits: {:032b}", u);
fn new_linear_congruential_size(size: u32) -> Option<RandState<'a>>
[src]
Creates a new random generator with a linear congruential
algorithm like the new_linear_congruential
method.
For the linear congrentail algorithm X = (a × X + c)
mod 2bits, a, c and bits are selected from
a table such that at least size bits of each X will be
used, that is bits ≥ size. The table only has values for a
size of up to 256 bits; None
will be returned if the
requested size is larger.
Examples
use rug::rand::RandState; let mut rand = match RandState::new_linear_congruential_size(100) { Some(r) => r, None => unreachable!(), }; let u = rand.bits(32); println!("32 random bits: {:032b}", u);
fn new_custom<'c, T>(custom: &mut T) -> RandState<'c> where
T: 'c + RandGen,
[src]
T: 'c + RandGen,
Creates a new custom random generator.
The created RandState
is borrowing mutably, so unlike other
instances of RandState
, it cannot be cloned; attempting to
clone will panic.
Examples
use rug::Integer; use rug::rand::{RandGen, RandState}; struct Seed; impl RandGen for Seed { fn gen(&mut self) -> u32 { 0x8cef7310 } } let mut seed = Seed; let mut rand = RandState::new_custom(&mut seed); let mut i = Integer::from(15); i.random_below_mut(&mut rand); println!("0 ≤ {} < 15", i); assert!(i < 15);
fn seed(&mut self, seed: &Integer)
[src]
Seeds the random generator.
Examples
use rug::Integer; use rug::rand::RandState; let seed = Integer::from(123456); let mut rand = RandState::new(); rand.seed(&seed); let u1a = rand.bits(32); let u1b = rand.bits(32); // reseed with the same seed rand.seed(&seed); let u2a = rand.bits(32); let u2b = rand.bits(32); assert_eq!(u1a, u2a); assert_eq!(u1b, u2b);
fn bits(&mut self, bits: u32) -> u32
[src]
Generates a random number with the specified number of bits.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let u = rand.bits(16); assert!(u < (1 << 16)); println!("16 random bits: {:016b}", u);
Panics
Panics if bits
is greater than 32.
fn below(&mut self, bound: u32) -> u32
[src]
Generates a random number below the given boundary value.
This function can never return the maximum 32-bit value; in
order to generate a 32-bit random value that covers the whole
range, use the bits
method with bits
set
to 32.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let u = rand.below(10000); assert!(u < 10000); println!("0 ≤ {} < 10000", u);
Panics
Panics if the boundary value is zero.
Trait Implementations
impl<'a> Default for RandState<'a>
[src]
impl<'a> Clone for RandState<'a>
[src]
fn clone(&self) -> RandState<'a>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more