use std::num::{Wrapping};
use std::time::{SystemTime, UNIX_EPOCH};
pub struct Rng {
seed: Wrapping<u8>,
w: Wrapping<u8>,
x: Wrapping<u8>,
}
impl Rng {
pub fn default() -> Rng {
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
Rng {
seed: Wrapping(seed.subsec_nanos() as u8),
x: Wrapping(0),
w: Wrapping(0)
}
}
pub fn new(seed: u8) -> Rng {
Rng {
seed: Wrapping(seed),
x: Wrapping(0),
w: Wrapping(0)
}
}
pub fn next(&mut self) -> u8 {
self.w += self.seed;
self.x *= self.x;
self.x += self.w;
(self.x >> 4 | self.x << 4).0
}
}