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
#![no_std]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rand32 {
state: u64,
inc: u64,
}
impl Rand32 {
pub fn new(seed: u64) -> Self {
let mut rng = Self {
state: 0,
inc: (0xda3e39cb94b95bdb << 1) | 1,
};
let _ = rng.rand();
rng.state += seed;
let _ = rng.rand();
rng
}
pub fn rand(&mut self) -> u32 {
let oldstate: u64 = self.state;
self.state = oldstate * 6364136223846793005 + self.inc;
let xorshifted: u32 = (((oldstate >> 18) ^ oldstate) >> 27) as u32;
let rot: u32 = (oldstate >> 59) as u32;
(xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rand64 {
state: u128,
inc: u128,
}
impl Rand64 {
pub fn new(seed: u128) -> Self {
let mut rng = Self {
state: 0,
inc: (63641362238467930051442695040888963407 << 1) | 1,
};
let _ = rng.rand();
rng.state += seed;
let _ = rng.rand();
rng
}
pub fn rand(&mut self) -> u64 {
let oldstate: u128 = self.state;
self.state = oldstate * 25492979953554139244865540595714422341 + self.inc;
let xorshifted: u64 = (((oldstate >> 29) ^ oldstate) >> 58) as u64;
let rot: u64 = (oldstate >> 112) as u64;
(xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}