#[derive(Clone, Debug)]
pub struct Rng(u64);
impl Rng {
pub fn new(seed: u64) -> Self {
Rng(seed)
}
pub fn fork(&self, tag: u64) -> Rng {
let mut r = Rng(self.0 ^ tag.wrapping_mul(0xD6E8_FEB8_6659_FD93));
r.next_u64();
r
}
pub fn next_u64(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
pub fn frand(&mut self) -> f32 {
(self.next_u64() >> 40) as f32 / 16_777_216.0
}
pub fn below(&mut self, n: u32) -> u32 {
if n == 0 {
return 0;
}
(((self.next_u64() >> 32) * n as u64) >> 32) as u32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deterministic_and_in_range() {
let mut a = Rng::new(1);
let mut b = Rng::new(1);
for _ in 0..1000 {
let x = a.frand();
assert_eq!(x, b.frand());
assert!((0.0..1.0).contains(&x));
}
let mut c = Rng::new(2);
assert_ne!(Rng::new(1).next_u64(), c.next_u64());
for _ in 0..1000 {
assert!(a.below(7) < 7);
}
}
}