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 = "*"
```rust extern crate randomchoice; use self::randomchoice::random_choice;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec
random_choice().random_choice_in_place_f64(&mut samples, &weights);
for sample in samples {
print!("{}, ", sample);
}
} ```
```rust extern crate randomchoice; use self::randomchoice::random_choice;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec
let number_choices = 100;
let choices = random_choice().random_choice_f64(&samples, &weights, number_choices);
for choice in choices {
print!("{}, ", choice);
}
} ```
```rust extern crate random_choice; extern crate rand;
use self::randomchoice::RandomChoice; use self::rand::threadrng;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec
let mut random_choice = RandomChoice::new(thread_rng());
random_choice.random_choice_in_place_f64(&mut samples, &weights);
for sample in samples {
print!("{}, ", sample);
}
} ```