1use std::cell::Cell;
11
12#[derive(Debug)]
13pub(crate) struct FastRand {
14 one: Cell<u32>,
15 two: Cell<u32>,
16}
17
18impl FastRand {
19 pub(crate) fn new(seed: u64) -> FastRand {
21 let one = (seed >> 32) as u32;
22 let mut two = seed as u32;
23
24 if two == 0 {
25 two = 1;
27 }
28
29 FastRand {
30 one: Cell::new(one),
31 two: Cell::new(two),
32 }
33 }
34
35 pub(crate) fn fastrand_n(&self, n: u32) -> u32 {
36 let mul = (self.fastrand() as u64).wrapping_mul(n as u64);
39 (mul >> 32) as u32
40 }
41
42 fn fastrand(&self) -> u32 {
43 let mut s1 = self.one.get();
44 let s0 = self.two.get();
45
46 s1 ^= s1 << 17;
47 s1 = s1 ^ s0 ^ (s1 >> 7) ^ (s0 >> 16);
48
49 self.one.set(s0);
50 self.two.set(s1);
51
52 s0.wrapping_add(s1)
53 }
54}
55
56pub fn thread_rng_n(n: u32) -> u32 {
58 thread_local! {
59 static THREAD_RNG: FastRand = FastRand::new(seed());
60 }
61
62 THREAD_RNG.with(|rng| rng.fastrand_n(n))
63}
64
65use std::{
66 collections::hash_map::RandomState,
67 hash::BuildHasher,
68 sync::atomic::{AtomicU32, Ordering::Relaxed},
69};
70
71static COUNTER: AtomicU32 = AtomicU32::new(1);
72
73fn seed() -> u64 {
74 let rand_state = RandomState::new();
75 rand_state.hash_one(COUNTER.fetch_add(1, Relaxed))
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn rand() {
84 for _ in 0..100 {
85 assert!(thread_rng_n(10) < 10);
86 }
87 }
88}