1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::cell::Cell;
use std::sync::Mutex;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash, Hasher};
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
static COUNTER: AtomicU32 = AtomicU32::new(1);
pub(crate) fn seed() -> u64 {
let rand_state = RandomState::new();
let mut hasher = rand_state.build_hasher();
COUNTER.fetch_add(1, Relaxed).hash(&mut hasher);
hasher.finish()
}
#[repr(C)]
#[derive(Debug)]
pub struct RngSeedGenerator {
state: Mutex<FastRand>,
}
impl RngSeedGenerator {
pub fn new(seed: RngSeed) -> Self {
Self {
state: Mutex::new(FastRand::new(seed)),
}
}
pub fn next_seed(&self) -> RngSeed {
let rng = self
.state
.lock()
.expect("RNG seed generator is internally corrupt");
let s = rng.fastrand();
let r = rng.fastrand();
RngSeed::from_pair(s, r)
}
pub fn next_generator(&self) -> Self {
RngSeedGenerator::new(self.next_seed())
}
}
impl Default for RngSeedGenerator {
fn default() -> Self {
Self::new(RngSeed::new())
}
}
#[allow(unreachable_pub)]
#[derive(Clone, Debug)]
pub struct RngSeed {
s: u32,
r: u32,
}
impl RngSeed {
pub fn new() -> Self {
Self::from_u64(seed())
}
fn from_u64(seed: u64) -> Self {
let one = (seed >> 32) as u32;
let mut two = seed as u32;
if two == 0 {
two = 1;
}
Self::from_pair(one, two)
}
fn from_pair(s: u32, r: u32) -> Self {
Self { s, r }
}
}
impl Default for RngSeed {
fn default() -> Self {
Self::new()
}
}
#[repr(C)]
#[derive(Debug)]
pub struct FastRand {
one: Cell<u32>,
two: Cell<u32>,
}
impl FastRand {
pub fn new(seed: RngSeed) -> FastRand {
FastRand {
one: Cell::new(seed.s),
two: Cell::new(seed.r),
}
}
pub fn replace_seed(&self, seed: RngSeed) -> RngSeed {
let old_seed = RngSeed::from_pair(self.one.get(), self.two.get());
self.one.replace(seed.s);
self.two.replace(seed.r);
old_seed
}
pub fn fastrand_n(&self, n: u32) -> u32 {
let mul = (self.fastrand() as u64).wrapping_mul(n as u64);
(mul >> 32) as u32
}
fn fastrand(&self) -> u32 {
let mut s1 = self.one.get();
let s0 = self.two.get();
s1 ^= s1 << 17;
s1 = s1 ^ s0 ^ s1 >> 7 ^ s0 >> 16;
self.one.set(s0);
self.two.set(s1);
s0.wrapping_add(s1)
}
}