logo
pub fn sample<R>(rng: &mut R, length: usize, amount: usize) -> IndexVecwhere
    R: Rng + ?Sized,
Available on crate feature alloc only.
Expand description

Randomly sample exactly amount distinct indices from 0..length, and return them in random order (fully shuffled).

This method is used internally by the slice sampling methods, but it can sometimes be useful to have the indices themselves so this is provided as an alternative.

The implementation used is not specified; we automatically select the fastest available algorithm for the length and amount parameters (based on detailed profiling on an Intel Haswell CPU). Roughly speaking, complexity is O(amount), except that when amount is small, performance is closer to O(amount^2), and when length is close to amount then O(length).

Note that performance is significantly better over u32 indices than over u64 indices. Because of this we hide the underlying type behind an abstraction, IndexVec.

If an allocation-free no_std function is required, it is suggested to adapt the internal sample_floyd implementation.

Panics if amount > length.