DiscreteDistribution

Struct DiscreteDistribution 

Source
pub struct DiscreteDistribution<'a, T: Probability> { /* private fields */ }
Expand description

Represents empirical discrete distribution.

Implementations§

Source§

impl<'_a, T: Probability> DiscreteDistribution<'_a, T>

Source

pub fn new(items: &'_a Vec<T>, method: SamplingMethod) -> Result<Self>

Returns DiscreteDistribution based on the selection method.

use proportionate_selector::*;

#[derive(PartialEq)]
pub struct MultiVariantMarketingWebsiteItem {
    pub id: i32,
    /// Likelihood of recieve marketing website version.
    /// Rarity represents inverse lilihood of recieveing
    /// this item.
    ///
    /// e.g. rairity of 1, means website item item will be more
    /// frequently generated as opposed to rarity of 100.
    pub rarity: f64,
}

impl Probability for MultiVariantMarketingWebsiteItem {
    fn prob(&self) -> f64 {
        // rarity is modeled as 1 out of X occurance, so
        // rarity of 20 has probability of 1/20.
        1.0 / self.rarity
    }
}

let summer2020Launch = vec![
    MultiVariantMarketingWebsiteItem {id: 0, rarity: 5.0}, // 20%
    MultiVariantMarketingWebsiteItem {id: 1, rarity: 5.0}, // 20%
    MultiVariantMarketingWebsiteItem {id: 2, rarity: 10.0}, // 10%
    MultiVariantMarketingWebsiteItem {id: 3, rarity: 2.5}, // 40%
    MultiVariantMarketingWebsiteItem {id: 4, rarity: 10.0}, // 40%
];

// create distribution for sampling
let epdf = DiscreteDistribution::new(&summer2020Launch, SamplingMethod::Linear);
assert!(epdf.is_ok());
Source

pub fn sample(&'_a self) -> Option<&T>

Returns a sample based on discrete distribution.

As invocation of sample() reaches large number (e.g. +infinity), the difference between population (defined discrete distribution), and distribution from generated sample diminishes.

use proportionate_selector::*;

#[derive(PartialEq)]
pub struct LootboxItem {
    pub id: i32,
    /// Likelihood of recieve item from lootbox.
    /// Rarity represents inverse lilihood of recieveing
    /// this item.
    ///
    /// e.g. rairity of 1, means lootbox item will be more
    /// frequently generated as opposed to rarity of 100.
    pub rarity: f64,
}

impl Probability for LootboxItem {
    fn prob(&self) -> f64 {
        // rarity is modeled as 1 out of X occurance, so
        // rarity of 20 has probability of 1/20.
        1.0 / self.rarity
    }
}

let endOfLevel1Box = vec![
    LootboxItem {id: 0, rarity: 5.0}, // 20%
    LootboxItem {id: 1, rarity: 5.0}, // 20%
    LootboxItem {id: 2, rarity: 10.0}, // 10%
    LootboxItem {id: 3, rarity: 2.5}, // 40%
    LootboxItem {id: 4, rarity: 10.0}, // 40%
];

// create discrete distribution for sampling
let epdf = DiscreteDistribution::new(&endOfLevel1Box, SamplingMethod::Linear).unwrap();
let s = epdf.sample();

assert!(s.is_some());

Auto Trait Implementations§

§

impl<'a, T> Freeze for DiscreteDistribution<'a, T>

§

impl<'a, T> RefUnwindSafe for DiscreteDistribution<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for DiscreteDistribution<'a, T>
where T: Sync,

§

impl<'a, T> Sync for DiscreteDistribution<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for DiscreteDistribution<'a, T>

§

impl<'a, T> UnwindSafe for DiscreteDistribution<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V