Skip to main content

sample_weighted

Function sample_weighted 

Source
pub fn sample_weighted<R, F>(
    rng: &mut R,
    length: usize,
    weight: F,
    amount: usize,
) -> Vec<usize>
where R: Rng + ?Sized, F: Fn(usize) -> f64,
Expand description

Draws amount distinct indices from 0..length with probability proportional to weight, in descending order of the keys drawn.

Efraimidis and Spirakis, https://doi.org/10.1016/j.ipl.2005.11.003: give index i the key u^(1/w_i) for a uniform u, then take the largest keys. Passing amount == length therefore yields a weighted permutation of every index.

Panics if any weight is negative or not a number.