Chooses samples randomly by their weights/probabilities.
This algorithm is based on the stochastic universal sampling algorithm.
Add this to your Cargo.toml
:
toml
[dependencies]
random_choice = "*"
``` extern crate randomchoice; use self::randomchoice::random_choice;
let capacity: usize = 500;
let mut samples: Vec
for i in 0..capacity { samples.push(i); weights.push(i as f64); }
let numberchoices = 10000; let choices = randomchoice().randomchoicef64(&samples, &weights, number_choices);
assert!(choices.len() == number_choices);
for choice in choices { print!("{}, ", choice); }
```
``` extern crate rand; extern crate randomchoice; use randomchoice::RandomChoice; use rand::SeedableRng;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec
let rng = rand::StdRng::from_seed(&[5000, 44, 55, 199]);
let mut random_choice = RandomChoice::new(rng);
let number_choices = 100;
let choices = random_choice.random_choice_f64(&mut samples, &weights, number_choices);
for choice in choices {
print!("{}, ", choice);
}
} ```