Struct rand::distributions::WeightedChoice [−][src]
pub struct WeightedChoice<'a, T: 'a> { /* fields omitted */ }
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 Distribution
trait.
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, Distribution}; 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.sample(&mut rng)); }
Methods
impl<'a, T: Clone> WeightedChoice<'a, T>
[src]
impl<'a, T: Clone> WeightedChoice<'a, T>
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:
items
is empty- the total weight is 0
- the total weight is larger than a
u32
can contain.
Trait Implementations
impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T>
[src]
impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T>
fn sample<R: Rng>(&mut self, rng: &mut R) -> T
[src]
fn sample<R: Rng>(&mut self, rng: &mut R) -> T
: use Distribution instead
Generate a random value of Support
, using rng
as the source of randomness. Read more
impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T>
[src]
impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T>
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T
[src]
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T
: use Distribution instead
Generate a random value.
impl<'a, T: Debug + 'a> Debug for WeightedChoice<'a, T>
[src]
impl<'a, T: Debug + 'a> Debug for WeightedChoice<'a, T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a, T: Clone> Distribution<T> for WeightedChoice<'a, T>
[src]
impl<'a, T: Clone> Distribution<T> for WeightedChoice<'a, T>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T
[src]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T
Generate a random value of T
, using rng
as the source of randomness.
ⓘImportant traits for DistIter<'a, D, R, T>fn sample_iter<'a, R>(&'a self, rng: &'a mut R) -> DistIter<'a, Self, R, T> where
Self: Sized,
R: Rng,
[src]
fn sample_iter<'a, R>(&'a self, rng: &'a mut R) -> DistIter<'a, Self, R, T> where
Self: Sized,
R: Rng,
Create an iterator that generates random values of T
, using rng
as the source of randomness. Read more
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,