Skip to main content

sample_efraimidis_spirakis

Function sample_efraimidis_spirakis 

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

Randomly sample amount distinct indices from 0..length

The result may contain less than amount indices if insufficient non-zero weights are available. Results are returned in an arbitrary order (there is no guarantee of shuffling or ordering).

Function weight is called once for each index to provide weights.

This implementation is based on the algorithm A-ExpJ as found in Efraimidis and Spirakis, 2005. It uses O(length + amount) space and O(length) time.

Error cases:

  • WeightError::InvalidWeight when a weight is not-a-number or negative, or when infinite weights fill the reservoir before all elements have been processed (this always happens with more than amount infinite weights, and may also happen with exactly amount, depending on order).