Trait tiamat::RandomGen [−][src]
pub trait RandomGen { fn gen<R: Random>(&mut self) -> R
where
Self: Sized, { ... } fn build<B, R>(&mut self, builder: &B) -> R
where
B: BuildRandom<R>,
Self: Sized, { ... } fn choose_from<'a, T>(&mut self, xs: &'a [T]) -> &'a T
where
Self: Sized, { ... } fn iter_gen<R: Random>(&mut self) -> IterGen<Self, R>
where
Self: Sized, { ... } fn iter_build<'a, B: 'a, R>(
&'a mut self,
builder: &'a B
) -> IterBuild<'a, Self, B, R>
where
B: BuildRandom<R>,
Self: Sized, { ... } fn iter_choose_from<'a, T>(
&'a mut self,
xs: &'a [T]
) -> IterChooseFrom<'a, Self, T>
where
Self: Sized, { ... } fn gen_u32(&mut self) -> u32 { ... } fn gen_u64(&mut self) -> u64 { ... } fn fill_buffer(&mut self, buffer: &mut [u8]) { ... } }
A (pseudo-)random number generator.
A RandomGen
provides facilities to generate (pseudo-)random numbers.
Even though it explicitly provides methods for generating u32
and u64
values, it is suggested to use gen
and let the type checker infer the
appropriate type to be generated.
Implementing RandomGen
Instances must implement at least one of gen_u32
or gen_u64
, each
one has a default implementation in terms of the other. You might want to
implement both if there is a more efficient way to do so.
Note that the output of Random
and BuildRandom
instances can only be
as good as their source.
Examples
use tiamat::RandomGen; let mut rng = get_rng(); println!("{}", if rng.gen() { "Heads!" } else { "Tails!" }); println!("The dice landed on {}", rng.build(&(1..7))); if rng.build(&(1.0 / 36.0)) { println!("Snake eyes!"); } else { println!("No snake eyes :("); }
Provided Methods
fn gen<R: Random>(&mut self) -> R where
Self: Sized,
Self: Sized,
Generates a random value.
Wrapper around Random::random
, but allowing for type inference.
See Random::random
's documentation (and the documentation of
Random
's instances) for more.
fn build<B, R>(&mut self, builder: &B) -> R where
B: BuildRandom<R>,
Self: Sized,
B: BuildRandom<R>,
Self: Sized,
Generates a random value according to some specification.
Wrapper around BuildRandom::build
, but allowing for type inference.
The argument is an instance of Borrow<B : BuildRandom<R>>
to allow for
passing both &B
and B
(etc.).
See BuildRandom::build
's documentation (and the documentation of
BuildRandom
's instances) for more.
fn choose_from<'a, T>(&mut self, xs: &'a [T]) -> &'a T where
Self: Sized,
Self: Sized,
Chooses a random element from the slice, where each element has the same probability of being chosen.
Panics
Panics if the slice has length 0, so that no element could be chosen.
Examples
use tiamat::RandomGen; let mut rng = get_rng(); let fav_color = rng.choose_from(&["red", "green", "purple"]); println!("favorite color is {}", fav_color);
fn iter_gen<R: Random>(&mut self) -> IterGen<Self, R> where
Self: Sized,
Self: Sized,
Returns an iterator over randomly generated values.
This is the iterating version of gen
.
Examples
use tiamat::RandomGen; let mut rng = get_rng(); let coin_tosses: Vec<&str> = rng.iter_gen() .map(|tails| if tails { "tails" } else { "heads" }) .take(10) .collect(); println!("{:?}", coin_tosses);
fn iter_build<'a, B: 'a, R>(
&'a mut self,
builder: &'a B
) -> IterBuild<'a, Self, B, R> where
B: BuildRandom<R>,
Self: Sized,
&'a mut self,
builder: &'a B
) -> IterBuild<'a, Self, B, R> where
B: BuildRandom<R>,
Self: Sized,
Returns an iterator over randomly built values.
This is the iterating version of build
.
Examples
use tiamat::RandomGen; let mut rng = get_rng(); let dice_throws: Vec<u8> = rng.iter_build(&(1..7)) .take(10) .collect(); println!("dice_throws: {:?}", dice_throws);
fn iter_choose_from<'a, T>(
&'a mut self,
xs: &'a [T]
) -> IterChooseFrom<'a, Self, T> where
Self: Sized,
&'a mut self,
xs: &'a [T]
) -> IterChooseFrom<'a, Self, T> where
Self: Sized,
Returns an iterator over values randomly chosen from a slice.
This is the iterating version of choose_from
. Note that this means
that each choice is made independantly.
Panics
Panics if xs
is empty, so that no elements can be chosen.
fn gen_u32(&mut self) -> u32
Returns a (pseudo-)random, uniformly distributed u32
.
Either this or gen_u64
must be implemented by an implementing type.
When using RandomGen
, it is recommended to use gen
instead and
let the type checker infer the type (or write [gen::<u32>
]).
fn gen_u64(&mut self) -> u64
Returns a (pseudo-)random, uniformly distributed u64
.
Either this or gen_u32
must be implemented by an implementing type.
When using RandomGen
, it is recommended to use gen
instead and
let the type checker infer the type (or write gen::<u64>
).
fn fill_buffer(&mut self, buffer: &mut [u8])
Fills the buffer with (pseudo-)random bytes.
Examples
use tiamat::RandomGen; let mut rng = get_rng(); let mut garbage = Vec::with_capacity(128); rng.fill_buffer(&mut garbage); println!("{:?}", garbage);