use rand_core::{utils, TryRng};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StepRng {
v: u64,
a: u64,
}
impl StepRng {
pub fn new(initial: u64, increment: u64) -> Self {
StepRng { v: initial, a: increment }
}
}
impl TryRng for StepRng {
type Error = std::convert::Infallible;
#[inline]
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
self.try_next_u64().map(|n| n as u32)
}
#[inline]
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
let res = self.v;
self.v = self.v.wrapping_add(self.a);
Ok(res)
}
#[inline]
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
utils::fill_bytes_via_next_word(dst, || self.try_next_u64());
Ok(())
}
}