summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/stdx/src/rand.rs
blob: 64aa57eae09cd9ac27483c5d4c2ce71042c4ee0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//! We don't use `rand`, as that's too many things for us.
//!
//! We currently use oorandom instead, but it's missing these two utilities.
//! Perhaps we should switch to `fastrand`, or our own small PRNG, it's not like
//! we need anything more complicated than xor-shift.

pub fn shuffle<T>(slice: &mut [T], mut rand_index: impl FnMut(usize) -> usize) {
    let mut remaining = slice.len() - 1;
    while remaining > 0 {
        let index = rand_index(remaining);
        slice.swap(remaining, index);
        remaining -= 1;
    }
}

pub fn seed() -> u64 {
    use std::collections::hash_map::RandomState;
    use std::hash::{BuildHasher, Hasher};

    RandomState::new().build_hasher().finish()
}