Trait rand::SampleRng 
                   
                       [−]
                   
               [src]
pub trait SampleRng: Rng {
    fn gen<T: Rand>(&mut self) -> T
    where
        Self: Sized,
    { ... }
    fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self>
    where
        Self: Sized,
    { ... }
    fn gen_range<T: PartialOrd + SampleRange>(&mut self, low: T, high: T) -> T
    where
        Self: Sized,
    { ... }
    fn gen_weighted_bool(&mut self, n: u32) -> bool
    where
        Self: Sized,
    { ... }
    fn gen_ascii_chars<'a>(&'a mut self) -> AsciiGenerator<'a, Self>
    where
        Self: Sized,
    { ... }
    fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T>
    where
        Self: Sized,
    { ... }
    fn choose_mut<'a, T>(&mut self, values: &'a mut [T]) -> Option<&'a mut T>
    where
        Self: Sized,
    { ... }
    fn shuffle<T>(&mut self, values: &mut [T])
    where
        Self: Sized,
    { ... }
}An automatically-implemented extension trait on Rng providing high-level
generic methods for sampling values and other convenience methods.
Users should "use" this trait to enable its extension methods on Rng
or require this type directly (i.e. <R: SampleRng>). Since SampleRng
extends Rng and every Rng implements SampleRng, usage of the two
traits is somewhat interchangeable.
This functionality is provided as an extension trait to allow separation
between the backend (the Rng providing randomness) and the front-end
(converting that randomness to the desired type and distribution).
Provided Methods
fn gen<T: Rand>(&mut self) -> T where
    Self: Sized, 
Self: Sized,
Return a random value of a Rand type.
Example
use rand::{thread_rng, SampleRng}; let mut rng = thread_rng(); let x: u32 = rng.gen(); println!("{}", x); println!("{:?}", rng.gen::<(f64, bool)>());
fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self> where
    Self: Sized, 
Self: Sized,
Return an iterator that will yield an infinite number of randomly generated items.
Example
use rand::{thread_rng, SampleRng}; let mut rng = thread_rng(); let x = rng.gen_iter::<u32>().take(10).collect::<Vec<u32>>(); println!("{:?}", x); println!("{:?}", rng.gen_iter::<(f64, bool)>().take(5) .collect::<Vec<(f64, bool)>>());
fn gen_range<T: PartialOrd + SampleRange>(&mut self, low: T, high: T) -> T where
    Self: Sized, 
Self: Sized,
Generate a random value in the range [low, high).
This is a convenience wrapper around
distributions::Range. If this function will be called
repeatedly with the same arguments, one should use Range, as
that will amortize the computations that allow for perfect
uniformity, as they only happen on initialization.
Panics
Panics if low >= high.
Example
use rand::{thread_rng, SampleRng}; let mut rng = thread_rng(); let n: u32 = rng.gen_range(0, 10); println!("{}", n); let m: f64 = rng.gen_range(-40.0f64, 1.3e5f64); println!("{}", m);
fn gen_weighted_bool(&mut self, n: u32) -> bool where
    Self: Sized, 
Self: Sized,
Return a bool with a 1 in n chance of true
Example
use rand::{thread_rng, SampleRng}; let mut rng = thread_rng(); println!("{}", rng.gen_weighted_bool(3));
fn gen_ascii_chars<'a>(&'a mut self) -> AsciiGenerator<'a, Self> where
    Self: Sized, 
Self: Sized,
Return an iterator of random characters from the set A-Z,a-z,0-9.
Example
use rand::{thread_rng, SampleRng}; let s: String = thread_rng().gen_ascii_chars().take(10).collect(); println!("{}", s);
fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> where
    Self: Sized, 
Self: Sized,
Return a random element from values.
Return None if values is empty.
Example
use rand::{thread_rng, SampleRng}; let choices = [1, 2, 4, 8, 16, 32]; let mut rng = thread_rng(); println!("{:?}", rng.choose(&choices)); assert_eq!(rng.choose(&choices[..0]), None);
fn choose_mut<'a, T>(&mut self, values: &'a mut [T]) -> Option<&'a mut T> where
    Self: Sized, 
Self: Sized,
Return a mutable pointer to a random element from values.
Return None if values is empty.
fn shuffle<T>(&mut self, values: &mut [T]) where
    Self: Sized, 
Self: Sized,
Shuffle a mutable slice in place.
This applies Durstenfeld's algorithm for the Fisher–Yates shuffle which produces an unbiased permutation.
Example
use rand::{thread_rng, SampleRng}; let mut rng = thread_rng(); let mut y = [1, 2, 3]; rng.shuffle(&mut y); println!("{:?}", y); rng.shuffle(&mut y); println!("{:?}", y);
Implementors
- impl<R: Rng> SampleRng for R