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
use {RngCore, SeedableRng};
#[derive(Debug, Clone, Copy)]
pub struct SplitMix64Rng {
state: u64,
}
impl SplitMix64Rng {
#[cfg(feature = "rand")]
pub fn new() -> Self {
use rand::Rng;
let seed = rand::rngs::OsRng.gen::<<Self as SeedableRng>::Seed>();
Self::from_seed(seed)
}
pub fn from_seed_u64(seed: u64) -> Self {
SplitMix64Rng { state: seed }
}
}
impl RngCore for SplitMix64Rng {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(0x9E3779B97F4A7C15);
let mut z: u64 = self.state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
z ^ (z >> 31)
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut ctr = 0;
let mut v = 0;
for d in dest.iter_mut() {
if ctr == 0 {
v = self.next_u64();
ctr = 7;
}
*d = v as u8;
v >>= 8;
ctr -= 1;
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for SplitMix64Rng {
type Seed = [u8; 8];
fn from_seed(seed: [u8; 8]) -> Self {
Self::from_seed_u64(u64::from_le_bytes(seed))
}
}