Trait IndexedMutRandom
trait IndexedMutRandom: IndexedRandom + IndexMut<usize>
Extension trait on indexable lists, providing random sampling methods.
This trait is implemented automatically for every type implementing
IndexedRandom and [std::ops::IndexMut<usize>].
Provided Methods
fn choose_mut<R>(self: &mut Self, rng: &mut R) -> Option<&mut <Self as >::Output> where R: Rng + ?SizedUniformly sample one element (mut)
Returns a mutable reference to one uniformly-sampled random element of the slice, or
Noneif the slice is empty.For slices, complexity is
O(1).fn choose_weighted_mut<R, F, B, X>(self: &mut Self, rng: &mut R, weight: F) -> Result<&mut <Self as >::Output, WeightError> where R: Rng + ?Sized, F: Fn(&<Self as >::Output) -> B, B: SampleBorrow<X>, X: SampleUniform + Weight + PartialOrd<X>Biased sampling for one element (mut)
Returns a mutable reference to one element of the slice, sampled according to the provided weights. Returns
Noneonly if the slice is empty.The specified function
weightmaps each itemxto a relative likelihoodweight(x). The probability of each item being selected is thereforeweight(x) / s, wheresis the sum of allweight(x).For slices of length
n, complexity isO(n). For more information about the underlying algorithm, see theWeightedIndexdistribution.See also
choose_weighted.
Implementors
impl<IR: IndexedRandom + IndexMut<usize> + ?Sized> IndexedMutRandom for IR