pub struct WeightedAliasIndex<W>where
W: AliasableWeight,{ /* private fields */ }Expand description
A distribution using weighted sampling to pick a discretely selected item.
Sampling a WeightedAliasIndex<W> distribution returns the index of a randomly
selected element from the vector used to create the WeightedAliasIndex<W>.
The chance of a given element being picked is proportional to the value of
the element. The weights can have any type W for which a implementation of
AliasableWeight exists.
§Performance
Given that n is the number of items in the vector used to create an
WeightedAliasIndex<W>, it will require O(n) amount of memory.
More specifically it takes up some constant amount of memory plus
the vector used to create it and a Vec<u32> with capacity n.
Time complexity for the creation of a WeightedAliasIndex<W> is O(n).
Sampling is O(1), it makes a call to Uniform<u32>::sample and a call
to Uniform<W>::sample.
§Example
use rand_distr::weighted::WeightedAliasIndex;
use rand::prelude::*;
let choices = vec!['a', 'b', 'c'];
let weights = vec![2, 1, 1];
let dist = WeightedAliasIndex::new(weights).unwrap();
let mut rng = rand::rng();
for _ in 0..100 {
// 50% chance to print 'a', 25% chance to print 'b', 25% chance to print 'c'
println!("{}", choices[dist.sample(&mut rng)]);
}
let items = [('a', 0), ('b', 3), ('c', 7)];
let dist2 = WeightedAliasIndex::new(items.iter().map(|item| item.1).collect()).unwrap();
for _ in 0..100 {
// 0% chance to print 'a', 30% chance to print 'b', 70% chance to print 'c'
println!("{}", items[dist2.sample(&mut rng)].0);
}Implementations§
Source§impl<W> WeightedAliasIndex<W>where
W: AliasableWeight,
impl<W> WeightedAliasIndex<W>where
W: AliasableWeight,
Sourcepub fn new(weights: Vec<W>) -> Result<WeightedAliasIndex<W>, Error>
pub fn new(weights: Vec<W>) -> Result<WeightedAliasIndex<W>, Error>
Creates a new WeightedAliasIndex.
Error cases:
Error::InvalidInputwhenweights.len()is zero or greater thanu32::MAX.Error::InvalidWeightwhen a weight is not-a-number, negative or greater thanmax = W::MAX / weights.len().Error::InsufficientNonZerowhen the sum of all weights is zero.
Trait Implementations§
Source§impl<W> Clone for WeightedAliasIndex<W>
impl<W> Clone for WeightedAliasIndex<W>
Source§fn clone(&self) -> WeightedAliasIndex<W>
fn clone(&self) -> WeightedAliasIndex<W>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<W> Debug for WeightedAliasIndex<W>
impl<W> Debug for WeightedAliasIndex<W>
Source§impl<W> Distribution<usize> for WeightedAliasIndex<W>where
W: AliasableWeight,
impl<W> Distribution<usize> for WeightedAliasIndex<W>where
W: AliasableWeight,
Auto Trait Implementations§
impl<W> Freeze for WeightedAliasIndex<W>
impl<W> RefUnwindSafe for WeightedAliasIndex<W>
impl<W> Send for WeightedAliasIndex<W>
impl<W> Sync for WeightedAliasIndex<W>
impl<W> Unpin for WeightedAliasIndex<W>
impl<W> UnwindSafe for WeightedAliasIndex<W>
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<D, T> DistributionExt<T> for Dwhere
D: Distribution<T>,
impl<D, T> DistributionExt<T> for Dwhere
D: Distribution<T>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.