[−][src]Struct rand::distributions::WeightedChoice
A distribution that selects from a finite collection of weighted items.
Each item has an associated weight that influences how likely it is to be chosen: higher weight is more likely.
The Clone restriction is a limitation of the Sample and
IndependentSample traits. Note that &T is (cheaply) Clone for
all T, as is u32, so one can store references or indices into
another vector.
Example
use rand::distributions::{Weighted, WeightedChoice, IndependentSample}; let mut items = vec!(Weighted { weight: 2, item: 'a' }, Weighted { weight: 4, item: 'b' }, Weighted { weight: 1, item: 'c' }); let wc = WeightedChoice::new(&mut items); let mut rng = rand::thread_rng(); for _ in 0..16 { // on average prints 'a' 4 times, 'b' 8 and 'c' twice. println!("{}", wc.ind_sample(&mut rng)); }
Methods
impl<'a, T> WeightedChoice<'a, T> where
T: Clone, [src]
impl<'a, T> WeightedChoice<'a, T> where
T: Clone, pub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T>[src]
pub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T>Create a new WeightedChoice.
Panics if:
itemsis empty- the total weight is 0
- the total weight is larger than a
u32can contain.
Trait Implementations
impl<'a, T> Debug for WeightedChoice<'a, T> where
T: 'a + Debug, [src]
impl<'a, T> Debug for WeightedChoice<'a, T> where
T: 'a + Debug, impl<'a, T> IndependentSample<T> for WeightedChoice<'a, T> where
T: Clone, [src]
impl<'a, T> IndependentSample<T> for WeightedChoice<'a, T> where
T: Clone, fn ind_sample<R>(&self, rng: &mut R) -> T where
R: Rng, [src]
fn ind_sample<R>(&self, rng: &mut R) -> T where
R: Rng, impl<'a, T> Sample<T> for WeightedChoice<'a, T> where
T: Clone, [src]
impl<'a, T> Sample<T> for WeightedChoice<'a, T> where
T: Clone, Auto Trait Implementations
impl<'a, T> Send for WeightedChoice<'a, T> where
T: Send,
impl<'a, T> Send for WeightedChoice<'a, T> where
T: Send, impl<'a, T> Sync for WeightedChoice<'a, T> where
T: Sync,
impl<'a, T> Sync for WeightedChoice<'a, T> where
T: Sync, Blanket Implementations
impl<T> From for T[src]
impl<T> From for Timpl<T, U> Into for T where
U: From<T>, [src]
impl<T, U> Into for T where
U: From<T>, impl<T, U> TryFrom for T where
T: From<U>, [src]
impl<T, U> TryFrom for T where
T: From<U>, type Error = !
🔬 This is a nightly-only experimental API. (
try_from)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>impl<T> Borrow for T where
T: ?Sized, [src]
impl<T> Borrow for T where
T: ?Sized, impl<T> Any for T where
T: 'static + ?Sized, [src]
impl<T> Any for T where
T: 'static + ?Sized, fn get_type_id(&self) -> TypeId[src]
fn get_type_id(&self) -> TypeIdimpl<T, U> TryInto for T where
U: TryFrom<T>, [src]
impl<T, U> TryInto for T where
U: TryFrom<T>, type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (
try_from)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>impl<T> BorrowMut for T where
T: ?Sized, [src]
impl<T> BorrowMut for T where
T: ?Sized, fn borrow_mut(&mut self) -> &mut T[src]
fn borrow_mut(&mut self) -> &mut T