| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
pub(crate) struct Rng(u64); |
| 14 |
|
| 15 |
impl Rng { |
| 16 |
pub(crate) fn new(seed: u64) -> Self { |
| 17 |
Self(seed) |
| 18 |
} |
| 19 |
|
| 20 |
pub(crate) fn next_u64(&mut self) -> u64 { |
| 21 |
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); |
| 22 |
let mut z = self.0; |
| 23 |
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); |
| 24 |
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); |
| 25 |
z ^ (z >> 31) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub(crate) fn coin(&mut self) -> bool { |
| 31 |
self.next_u64() >> 63 == 1 |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
pub(crate) fn below(&mut self, n: usize) -> usize { |
| 39 |
if n <= 1 { |
| 40 |
return 0; |
| 41 |
} |
| 42 |
((u128::from(self.next_u64()) * n as u128) >> 64) as usize |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(test)] |
| 47 |
mod tests { |
| 48 |
use super::Rng; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#[test] |
| 54 |
fn the_stream_is_splitmix64() { |
| 55 |
let mut rng = Rng::new(0); |
| 56 |
assert_eq!(rng.next_u64(), 0xE220_A839_7B1D_CDAF); |
| 57 |
assert_eq!(rng.next_u64(), 0x6E78_9E6A_A1B9_65F4); |
| 58 |
assert_eq!(rng.next_u64(), 0x06C4_5D18_8009_454F); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[test] |
| 64 |
fn below_stays_in_range() { |
| 65 |
let mut rng = Rng::new(12345); |
| 66 |
for n in [1usize, 2, 3, 80, 257] { |
| 67 |
for _ in 0..2000 { |
| 68 |
assert!(rng.below(n) < n, "below({n}) escaped its range"); |
| 69 |
} |
| 70 |
} |
| 71 |
assert_eq!(rng.below(0), 0); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#[test] |
| 78 |
fn the_coin_is_not_stuck() { |
| 79 |
let mut rng = Rng::new(99); |
| 80 |
let heads = (0..10_000).filter(|_| rng.coin()).count(); |
| 81 |
assert!( |
| 82 |
(4700..5300).contains(&heads), |
| 83 |
"10000 flips gave {heads} heads" |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|