Struct rand::distributions::WeightedChoice [−][src]
pub struct WeightedChoice<'a, T: 'a> { /* fields omitted */ }Expand description
A distribution that selects from a finite collection of weighted items.
Each item has an associated weight that influences how likely it is to be chosen: higher weight is more likely.
The Clone restriction is a limitation of the Sample and
IndependentSample traits. Note that &T is (cheaply) Clone for
all T, as is u32, so one can store references or indices into
another vector.
Example
use rand::distributions::{Weighted, WeightedChoice, IndependentSample};
let mut items = vec!(Weighted { weight: 2, item: 'a' },
Weighted { weight: 4, item: 'b' },
Weighted { weight: 1, item: 'c' });
let wc = WeightedChoice::new(&mut items);
let mut rng = rand::thread_rng();
for _ in 0..16 {
// on average prints 'a' 4 times, 'b' 8 and 'c' twice.
println!("{}", wc.ind_sample(&mut rng));
}Implementations
Trait Implementations
Generate a random value.
