Rust Random Choice

Chooses samples randomly by their weights/probabilities.

Advantages

This algorithm is based on the stochastic universal sampling algorithm.

Applications

Usage

Add this to your Cargo.toml:

toml [dependencies] random_choice = "*"

Examples

In Place Variant

```rust extern crate randomchoice; use self::randomchoice::random_choice;

fn main() { let mut samples = vec!["hi", "this", "is", "a", "test!"]; let weights: Vec = vec![5.6, 7.8, 9.7, 1.1, 2.0];

random_choice().random_choice_in_place_f64(&mut samples, &weights);

for sample in samples {
    print!("{}, ", sample);
}

} ```

N Selection Variant

```rust extern crate randomchoice; use self::randomchoice::random_choice;

fn main() { let mut samples = vec!["hi", "this", "is", "a", "test!"]; let weights: Vec = vec![5.6, 7.8, 9.7, 1.1, 2.0];

let number_choices = 100;
let choices = random_choice().random_choice_f64(&samples, &weights, number_choices);

for choice in choices {
    print!("{}, ", choice);
}

} ```

With Custom Seed

```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 = vec![5.6, 7.8, 9.7, 1.1, 2.0];

let mut random_choice = RandomChoice::new(thread_rng());
random_choice.random_choice_in_place_f64(&mut samples, &weights);

for sample in samples {
    print!("{}, ", sample);
}

} ```