Struct mendel::Bag[][src]

pub struct Bag<T: Clone> {
    pub items: Vec<T>,
    pub max_sims: u32,
}

The Bag struct. The main collection type for holding populations of things.

Fields

Methods

impl<T: Clone> Bag<T>
[src]

Constructs a new Bag<T> from range.

[min, max)

Examples

Generate a new Bag<i32> with numbers 1 through 10:

use mendel::Bag;

let my_number_bag = Bag::from_range(1, 11);

Constructs a new Bag<T> from a vector of items.

Examples

Generate a new Bag<&str>:

use mendel::Bag;

let animals = vec!["spider", "fish", "tiger", "pigeon"];
let animal_bag = Bag::from_vec(animals);

Predicts probability of criteria being met for the first random item grabbed from the bag.

Examples

Odds of selecting an even number from 1 - 10. Assert factors in +/- 1%:

use mendel::Bag;

let my_bag = Bag::from_range(1, 11);
let odds_of_even = my_bag.one(|v| v % 2 == 0);
assert!(0.49 < odds_of_even && odds_of_even < 0.51);

Predicts probability of criteria being met for the first sample_size random items grabbed from the bag.

Examples

Odds of getting a 2 in your first 3 picks from a list of numbers 1 - 10:

use mendel::Bag;

let my_bag = Bag::from_range(1, 11);
let odds_of_two = my_bag.sample(3, |values| {
    for v in values {
        if *v == 2 {
            return true;
        }
    }
    false
});
assert!(0.29 < odds_of_two && odds_of_two < 0.31);

Set the Bag's maximum amount of simulations to run when generating probabilities.

The default max_sims is set by either the MENDEL_MAX_SIMS environment variable value, or if that env var doesn't exist than it defaults to 100,000. In the future this may be a field that can be set during initialization of the Bag struct.

Examples

Set the max simulations to 10 thousand:

use mendel::Bag;

let mut my_bag = Bag::from_range(1, 11);
my_bag.set_max_sims(10_000);
assert!(my_bag.max_sims == 10_000);

Auto Trait Implementations

impl<T> Send for Bag<T> where
    T: Send

impl<T> Sync for Bag<T> where
    T: Sync