Struct sgx_rand::distributions::WeightedChoice
source · [−]pub struct WeightedChoice<'a, T: 'a> { /* private fields */ }
Expand description
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 sgx_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 = sgx_rand::thread_rng();
for _ in 0..16 {
// on average prints 'a' 4 times, 'b' 8 and 'c' twice.
println!("{}", wc.ind_sample(&mut rng));
}
Implementations
sourceimpl<'a, T: Clone> WeightedChoice<'a, T>
impl<'a, T: Clone> WeightedChoice<'a, T>
sourcepub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T>
pub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T>
Create a new WeightedChoice
.
Panics if:
v
is empty- the total weight is 0
- the total weight is larger than a
u32
can contain.
Trait Implementations
sourceimpl<'a, T: Debug + 'a> Debug for WeightedChoice<'a, T>
impl<'a, T: Debug + 'a> Debug for WeightedChoice<'a, T>
sourceimpl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T>
impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T>
sourcefn ind_sample<R: Rng>(&self, rng: &mut R) -> T
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T
Generate a random value.
Auto Trait Implementations
impl<'a, T> RefUnwindSafe for WeightedChoice<'a, T>where
T: RefUnwindSafe,
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> Unpin for WeightedChoice<'a, T>
impl<'a, T> !UnwindSafe for WeightedChoice<'a, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more