Crate choose_rand

Crate choose_rand 

Source
Expand description

§choose-rand

A small crate for choosing random items from a list of weighted items.

§Example

use choose_rand::prelude::*;

#[derive(Debug, Clone)]
struct Foo {
    prob: f32,
}

impl Probable for Foo {
    fn probability(&self) -> f32 {
        self.prob
    }
}

fn main() -> Result<()> {
    let v: Vec<_> = choose_rand::helper::refcellify(
        vec![Foo { prob: 0.25 }, Foo { prob: 0.5 }, Foo { prob: 0.1 }, Foo { prob: 0.05 }]
    ).collect();

    let mut rng = rand::thread_rng();    
    dbg!(v.choose_rand(&mut rng));

    Ok(())
}

Modules§

error
Contains the error enum for this crate.
helper
Contains some simple helper functions to make life easier when using this crate.
prelude
Contains all of the important things from this crate. When using the crate, you want to do use choose_rand::prelude::*;
rand
Adds all the main parts of the crate, including the traits required for the crate to work