use rand::prelude::*;
pub type RngType = rand::rngs::Xoshiro256PlusPlus;
const SEED: u64 = 7_355_608;
#[must_use]
#[inline]
pub fn seeded(seed: u64) -> RngType {
RngType::seed_from_u64(seed)
}
#[must_use]
#[inline]
pub fn deterministic() -> RngType {
seeded(SEED)
}
#[must_use]
#[inline]
pub fn random() -> RngType {
RngType::from_rng(&mut rand::rng())
}
#[cfg(test)]
pub struct HalfRng;
#[cfg(test)]
impl rand::TryRng for HalfRng {
type Error = std::convert::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(0x8000_0000)
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(0x8000_0000_0000_0000)
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
for chunk in dst.chunks_mut(8) {
chunk.copy_from_slice(&self.try_next_u64()?.to_le_bytes()[..chunk.len()]);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn half_rng() {
let mut rng = HalfRng;
assert_eq!(rng.random::<f32>().to_bits(), 0.5_f32.to_bits());
}
}