#[derive(Clone, Copy, Debug)]
pub struct Rng(u64);
impl Rng {
pub fn new(seed: u64) -> Self {
Rng(seed.wrapping_add(0x9e37_79b9_7f4a_7c15))
}
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 next_f64(&mut self) -> f64 {
(self.next_u64() >> 11) as f64 * (1.0 / (1u64 << 53) as f64)
}
pub fn next_f32(&mut self) -> f32 {
self.next_f64() as f32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deterministic_and_bounded() {
let mut a = Rng::new(7);
let mut b = Rng::new(7);
for _ in 0..100 {
assert_eq!(a.next_u64(), b.next_u64());
}
let mut c = Rng::new(7);
for _ in 0..100 {
let f = c.next_f64();
assert!((0.0..1.0).contains(&f));
}
}
}