Function rand::seq::index::sample_efraimidis_spirakis

source ·
fn sample_efraimidis_spirakis<R, F, X, N>(
    rng: &mut R,
    length: N,
    weight: F,
    amount: N,
) -> Result<IndexVec, WeightedError>
where R: Rng + ?Sized, F: Fn(usize) -> X, X: Into<f64>, N: UInt, IndexVec: From<Vec<N>>,
Expand description

Randomly sample exactly amount distinct indices from 0..length, and return them in an arbitrary order (there is no guarantee of shuffling or ordering). The weights are to be provided by the input function weights, which will be called once for each index.

This implementation uses the algorithm described by Efraimidis and Spirakis in this paper: https://doi.org/10.1016/j.ipl.2005.11.003 It uses O(length + amount) space and O(length) time if the “nightly” feature is enabled, or O(length) space and `O(length

  • amount * log length)` time otherwise.

Panics if amount > length.