pub use crate::pekkizen::*;
#[inline(always)]
pub fn unif_01(bits: impl FnMut() -> u64) -> f64 {
f64_full(bits)
}
#[cfg(feature = "rand")]
pub trait Unif01Ext {
fn unif_01(&mut self) -> f64;
}
#[cfg(feature = "rand")]
impl<R: rand_core::Rng + ?Sized> Unif01Ext for R {
#[inline(always)]
fn unif_01(&mut self) -> f64 {
f64_full(|| self.next_u64())
}
}
#[cfg(all(test, feature = "rand"))]
mod tests {
use super::*;
use crate::sources::Weyl;
struct WeylRng(Weyl);
impl rand_core::TryRng for WeylRng {
type Error = core::convert::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.0.next_u64() as u32)
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.0.next_u64())
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
for chunk in dst.chunks_mut(8) {
let bytes = self.0.next_u64().to_le_bytes();
chunk.copy_from_slice(&bytes[..chunk.len()]);
}
Ok(())
}
}
#[test]
fn test_unif_01_matches_f64_full() {
let mut rng = WeylRng(Weyl(42));
let mut src = Weyl(42);
let mut src2 = Weyl(42);
for _ in 0..100_000 {
let expected = f64_full(|| src.next_u64());
assert_eq!(rng.unif_01(), expected);
assert_eq!(unif_01(|| src2.next_u64()), expected);
}
}
}