pub struct DiscreteDistribution<'a, T: Probability> { /* private fields */ }Expand description
Represents empirical discrete distribution.
Implementations§
Source§impl<'_a, T: Probability> DiscreteDistribution<'_a, T>
impl<'_a, T: Probability> DiscreteDistribution<'_a, T>
Sourcepub fn new(items: &'_a Vec<T>, method: SamplingMethod) -> Result<Self>
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());Sourcepub fn sample(&'_a self) -> Option<&T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more