pub struct WeightedPicker<T> { /* private fields */ }
Expand description
It’s often helpful to have weighted probabilities. This struct serves as a sort of weighted bag; you can give it entries with various weights, and then randomly sample them.
This is the way Minecraft loot tables work, if this sounds familiar.
The algorithm used is Vose’s Alias Method (scroll to the bottom), which to be honest I absolutely do not understand. But it has O(n) creation and O(1) selection, so sounds good to me.
You can’t edit the probabilities after you’ve created it due to the algorithm.
Implementations§
Source§impl<T> WeightedPicker<T>
impl<T> WeightedPicker<T>
Sourcepub fn new(entries: Vec<(T, f64)>) -> Self
pub fn new(entries: Vec<(T, f64)>) -> Self
Initialize a WeightedPicker from the given items and weights.
Panics if you pass it an empty Vec.
let picker = WeightedPicker::new(vec![
("common", 10.0),
("uncommon", 5.0),
("rare", 2.0),
("legendary", 1.0),
("mythic", 0.1),
]);
let mut rng = rand::thread_rng();
for _ in 0..10 {
println!("- {}", picker.get(&mut rng));
}
/*
A sample output:
- legendary
- rare
- uncommon
- common
- common
- rare
- uncommon
- common
- common
- uncommon
*/
Sourcepub fn get_idx<R: Rng + ?Sized>(&self, rng: &mut R) -> usize
pub fn get_idx<R: Rng + ?Sized>(&self, rng: &mut R) -> usize
Get an index into the internal list.
This is like WeightedPicker::get
, but returns the index of the
selected value instead of the value.
You can use this function to save some space by passing a vec
where T
is ()
, if you want usize
outputs, I guess.
Sourcepub fn get_by_idx(&self, idx: usize) -> Option<&T>
pub fn get_by_idx(&self, idx: usize) -> Option<&T>
Manually index into the picker’s array.
Sourcepub fn get_mut_by_idx(&mut self, idx: usize) -> Option<&mut T>
pub fn get_mut_by_idx(&mut self, idx: usize) -> Option<&mut T>
Manually index into the picker’s array. You can use this to mutate entries once they’ve been created.
Note there is no way to mutate probabilities after creation, nor any way to add or remove possible values.
Trait Implementations§
Source§impl<T: Clone> Clone for WeightedPicker<T>
impl<T: Clone> Clone for WeightedPicker<T>
Source§fn clone(&self) -> WeightedPicker<T>
fn clone(&self) -> WeightedPicker<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl<T> Freeze for WeightedPicker<T>
impl<T> RefUnwindSafe for WeightedPicker<T>where
T: RefUnwindSafe,
impl<T> Send for WeightedPicker<T>where
T: Send,
impl<T> Sync for WeightedPicker<T>where
T: Sync,
impl<T> Unpin for WeightedPicker<T>where
T: Unpin,
impl<T> UnwindSafe for WeightedPicker<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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more