[−][src]Struct rug::rand::RandState
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<'_> RandState<'_>
[src]
pub fn new() -> RandState<'static>
[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);
pub fn new_mersenne_twister() -> RandState<'static>
[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);
pub fn new_linear_congruential(
a: &Integer,
c: u32,
bits: u32
) -> RandState<'static>
[src]
a: &Integer,
c: u32,
bits: u32
) -> RandState<'static>
Creates a new random generator with a linear congruential algorithm X = (a × X + c) mod 2bits.
Examples
use rug::rand::RandState; use rug::Integer; 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);
pub fn new_linear_congruential_size(size: u32) -> Option<RandState<'static>>
[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);
pub fn new_custom(custom: &mut dyn RandGen) -> RandState
[src]
Creates a new custom random generator.
If the custom random generator is cloned, the implemented
trait method RandGen::boxed_clone
is called; this leads to
panic if the method returns None
.
Examples
use rug::rand::{RandGen, RandState}; use rug::Integer; 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);
pub fn new_custom_boxed(custom: Box<dyn RandGen>) -> RandState<'static>
[src]
Creates a new custom random generator.
If the custom random generator is cloned, the implemented
trait method RandGen::boxed_clone
is called;
this leads to panic if the method returns None
.
Examples
use rug::rand::{RandGen, RandState}; use rug::Integer; struct Seed; impl RandGen for Seed { fn gen(&mut self) -> u32 { 0x8cef7310 } } let seed = Box::new(Seed); let mut rand = RandState::new_custom_boxed(seed); let mut i = Integer::from(15); i.random_below_mut(&mut rand); println!("0 ≤ {} < 15", i); assert!(i < 15);
pub unsafe fn from_raw(raw: randstate_t) -> RandState<'static>
[src]
Creates a random generator from an initialized GMP random generator.
Safety
- The value must be initialized. Note that the GMP functions
do not initialize all fields of the
gmp_mpfr_sys::gmp::randstate_t
object, which can eventually lead to reading uninitialized memory, and that is undefined behaviour in Rust even if no decision is made using the read value. One way to ensure that there is no uninitialized memory insideraw
is to usemem::zeroed
to initializeraw
before initializing with a function such asgmp_mpfr_sys::gmp::randinit_default
, like in the example below. - The
gmp_mpfr_sys::gmp::randstate_t
type can be considered as a kind of pointer, so there can be multiple copies of it. Since this function takes over ownership, no other copies of the passed value should exist.
Examples
use gmp_mpfr_sys::gmp; use rug::rand::RandState; use std::mem; let mut rand = unsafe { // Do not use mem::uninitialized, as gmp::randinit_default // does not initialize all of the fields of raw. let mut raw = mem::zeroed(); gmp::randinit_default(&mut raw); // raw is initialized and unique RandState::from_raw(raw) }; let u = rand.bits(32); println!("32 random bits: {:032b}", u); // since rand is a RandState now, deallocation is automatic
pub fn into_raw(self) -> randstate_t
[src]
Converts a random generator into a GMP random generator.
The returned object should be freed to avoid memory leaks.
Examples
use gmp_mpfr_sys::gmp; use rug::rand::RandState; let rand = RandState::new(); let mut raw = rand.into_raw(); unsafe { let u = gmp::urandomb_ui(&mut raw, 32) as u32; println!("32 random bits: {:032b}", u); // free object to prevent memory leak gmp::randclear(&mut raw); }
pub fn as_raw(&self) -> *const randstate_t
[src]
Returns a pointer to the inner GMP random generator.
The returned pointer will be valid for as long as self
is
valid.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let raw_ptr = rand.as_raw(); // There is not much you can do with an immutable randstate_t pointer. println!("pointer: {:p}", raw_ptr); let u = rand.bits(32); println!("32 random bits: {:032b}", u);
pub fn as_raw_mut(&mut self) -> *mut randstate_t
[src]
Returns an unsafe mutable pointer to the inner GMP random generator.
The returned pointer will be valid for as long as self
is
valid.
Examples
use gmp_mpfr_sys::gmp; use rug::rand::RandState; let mut rand = RandState::new(); let raw_ptr = rand.as_raw_mut(); unsafe { let u1 = gmp::urandomb_ui(raw_ptr, 32) as u32; println!("32 random bits: {:032b}", u1); } let u2 = rand.bits(32); println!("another 32 random bits: {:032b}", u2);
pub fn seed(&mut self, seed: &Integer)
[src]
Seeds the random generator.
Examples
use rug::rand::RandState; use rug::Integer; 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);
pub fn bits(&mut self, bits: u32) -> u32
[src]
Generates a random number with the specified number of bits.
Panics
Panics if bits
is greater than 32.
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);
pub 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.
Panics
Panics if the boundary value is zero.
Examples
use rug::rand::RandState; let mut rand = RandState::new(); let u = rand.below(10000); assert!(u < 10000); println!("0 ≤ {} < 10000", u);
Trait Implementations
impl<'_> Send for RandState<'_>
[src]
impl<'_> Sync for RandState<'_>
[src]
impl<'_> Default for RandState<'_>
[src]
impl<'_> Clone for RandState<'_>
[src]
fn clone(&self) -> RandState<'static>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<'_> Drop for RandState<'_>
[src]
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,