Random

Trait Random 

Source
pub trait Random: Debug {
    // Required methods
    fn generate_bool(&mut self, probability: f64) -> bool;
    fn generate_usize_in_range(&mut self, range: Range<usize>) -> usize;
    fn generate_i32_in_range(&mut self, lb: i32, ub: i32) -> i32;
    fn generate_f64(&mut self) -> f64;
    fn get_weighted_choice(&mut self, weights: &[f64]) -> Option<usize>;
}
Expand description

Abstraction for randomness, in order to swap out different source of randomness.

This is especially useful when testing, to control which variables are generated when random values are required.

§Testing

We have also created an implementation of this trait which takes as input a list of usizes and bools and returns them in that order. This allows the user to define deterministic test-cases while the implementation makes use of an implementation of the Random trait.

Required Methods§

Source

fn generate_bool(&mut self, probability: f64) -> bool

Generates a bool with probability probability of being true. It should hold that probability ∈ [0, 1], this method will panic if this is not the case.

§Example

This example will show how to use a concrete implementation of SeedableRng to implement a fair coin toss (e.g. a bernoulli trial with probability 0.5).

// First we create our random object
let mut rng = SmallRng::seed_from_u64(42);
// Then we flip a coin with probability 0.5
let coin_flip_outcome = rng.generate_bool(0.5);
// A sanity check to ensure that the method has not panicked
assert!(coin_flip_outcome || !coin_flip_outcome);

// If we provide a probability outside of the expected range then the method is expected to panic!
let result = std::panic::catch_unwind(|| rng.clone().generate_bool(120.0));
assert!(result.is_err());
Source

fn generate_usize_in_range(&mut self, range: Range<usize>) -> usize

Generates a random usize in the provided range with equal probability; this can be seen as sampling from a uniform distribution in the range [range.start, range.end)

§Example

This example will show how to use a concrete implementation of SeedableRng to implement selecting a random element from a list.

// First we create our random object
let mut rng = SmallRng::seed_from_u64(42);
// Then we create the elements to select from
let elements = vec!["This", "is", "a", "test"];
// Finally we generate a random number in the range [0, |elements|)
let selected_index = rng.generate_usize_in_range(0..elements.len());
assert!(selected_index >= 0 && selected_index < elements.len());
Source

fn generate_i32_in_range(&mut self, lb: i32, ub: i32) -> i32

Generates a random i32 in the provided range with equal probability; this can be seen as sampling from a uniform distribution in the range [lb, ub]

Source

fn generate_f64(&mut self) -> f64

Generate a random float in the range 0..1.

Source

fn get_weighted_choice(&mut self, weights: &[f64]) -> Option<usize>

Given a slice of weights, select the index with weight weighted probability compared to the other weights.

Implementors§

Source§

impl<T> Random for T
where T: SeedableRng + Rng + Debug,