Function malachite_base::slices::random_values_from_slice
source · pub fn random_values_from_slice<T>(
seed: Seed,
xs: &[T]
) -> RandomValuesFromSlice<'_, T> ⓘ
Expand description
Uniformly generates a random reference to a value from a nonempty slice.
The iterator cannot outlive the slice. It may be more convenient for the iterator to own the
data, in which case you may use random_values_from_vec
instead.
The output length is infinite.
$P(x) = 1/n$, where $n$ is xs.len()
.
Expected complexity per iteration
Constant time and additional memory.
Panics
Panics if xs
is empty.
Examples
use itertools::Itertools;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::slices::random_values_from_slice;
let xs = &[2, 3, 5, 7, 11];
assert_eq!(
random_values_from_slice(EXAMPLE_SEED, xs)
.cloned()
.take(10)
.collect_vec(),
&[3, 7, 3, 5, 11, 3, 5, 11, 2, 2]
);