stdx/rand.rs
1//! We don't use `rand` because that is too many things for us.
2//!
3//! `oorandom` is used instead, but it's missing these two utilities.
4//! Switching to `fastrand` or our own small PRNG may be good because only xor-shift is needed.
5
6pub fn shuffle<T>(slice: &mut [T], mut rand_index: impl FnMut(usize) -> usize) {
7 let mut remaining = slice.len() - 1;
8 while remaining > 0 {
9 let index = rand_index(remaining);
10 slice.swap(remaining, index);
11 remaining -= 1;
12 }
13}
14
15pub fn seed() -> u64 {
16 use std::hash::{BuildHasher, Hasher};
17 #[allow(clippy::disallowed_types)]
18 std::collections::hash_map::RandomState::new().build_hasher().finish()
19}