pub fn sample_multiple_from_known_length<I, R, T>(
rng: &mut R,
iter: I,
requested: usize,
) -> Vec<T>where
R: Rng,
I: IntoIterator<Item = T>,Expand description
Samples requested elements uniformly at random without replacement from an iterator
whose length is known at runtime. Requires len >= requested.
The caller must ensure that (len, Some(len)) == iter.size_hint(), i.e. the iterator
reports its exact length via size_hint. We do not require ExactSizeIterator
because that is a compile-time guarantee, whereas our requirement is a runtime condition.
The implementation selects random indices and uses Iterator::nth. For iterators
with O(1) nth (e.g., randomly indexable structures), this is very efficient.
Selected values are cloned.
This strategy is particularly effective for small requested (≤ 5), since it
avoids iterating over the entire set and is typically faster than reservoir sampling.