Function malachite_base::slices::random_slice_permutations
source · [−]pub fn random_slice_permutations<T>(
seed: Seed,
xs: &[T]
) -> RandomSlicePermutations<'_, T>ⓘNotable traits for RandomSlicePermutations<'a, T>impl<'a, T> Iterator for RandomSlicePermutations<'a, T> type Item = Vec<&'a T>;
Expand description
Uniformly generates a random permutation of references to a 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_vec_permutations
instead.
The output length is infinite.
$P(p) = 1/n!$, where $n$ is xs.len()
.
Expected complexity per iteration
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is xs.len()
.
Examples
extern crate itertools;
use itertools::Itertools;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::slices::random_slice_permutations;
let css: Vec<String> = random_slice_permutations(EXAMPLE_SEED, &['a', 'b', 'c', 'd'])
.take(20)
.map(|ds| ds.into_iter().copied().collect())
.collect();
assert_eq!(
css.iter().map(String::as_str).collect_vec().as_slice(),
[
"cadb", "cbad", "cadb", "badc", "acdb", "cbad", "dabc", "dbca", "cdba", "cdab", "bacd",
"cabd", "adbc", "cdab", "dcab", "abcd", "abcd", "dacb", "bcad", "adcb"
]
);