pub struct RandomSelector<T> { /* private fields */ }
Expand description
A selector allowing to randomly select a value from a set of choices, each with an associated weight.
use rand_select::RandomSelector;
let selector = RandomSelector::default()
.with(1.0, 'A')
.with(1.5, 'B')
.with_none(3.0);
let l = selector.select();
// l has half a chance to be None, and is 50% more likely to be 'B' than 'A'
Implementations§
Source§impl<T> RandomSelector<T>
impl<T> RandomSelector<T>
pub fn with(self, weight: f64, value: T) -> Self
pub fn with_none(self, weight: f64) -> Self
Sourcepub fn with_none_up_to(self, total_weight: f64) -> Self
pub fn with_none_up_to(self, total_weight: f64) -> Self
Complete choices to be None up to the given weight.
This is convenient where all choices set are conventionnaly already normalized:
use rand_select::RandomSelector;
let selector = RandomSelector::default()
.with(0.1, 'A')
.with(0.2, 'B')
.with_none_up_to(1.0);
Sourcepub fn select_with_rng<R: Rng>(&self, r: R) -> Option<&T>
pub fn select_with_rng<R: Rng>(&self, r: R) -> Option<&T>
Select a random value among the provided ones, with the generator of your choice.
Trait Implementations§
Source§impl<T: Clone> Clone for RandomSelector<T>
impl<T: Clone> Clone for RandomSelector<T>
Source§fn clone(&self) -> RandomSelector<T>
fn clone(&self) -> RandomSelector<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<T> Freeze for RandomSelector<T>
impl<T> RefUnwindSafe for RandomSelector<T>where
T: RefUnwindSafe,
impl<T> Send for RandomSelector<T>where
T: Send,
impl<T> Sync for RandomSelector<T>where
T: Sync,
impl<T> Unpin for RandomSelector<T>where
T: Unpin,
impl<T> UnwindSafe for RandomSelector<T>where
T: UnwindSafe,
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