From bada6377ee06f81c339609b7f34fe86285f1de71 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Thu, 1 Jul 2021 05:06:42 +0200 Subject: [PATCH] added a Rng abstraction around SplitMix64 --- src/dsp/helpers.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/dsp/helpers.rs b/src/dsp/helpers.rs index 8afa829..2fafe51 100644 --- a/src/dsp/helpers.rs +++ b/src/dsp/helpers.rs @@ -107,6 +107,26 @@ impl RandGen { } } +#[derive(Debug, Copy, Clone)] +pub struct Rng { + sm: SplitMix64, +} + +impl Rng { + pub fn new() -> Self { + Self { sm: SplitMix64::new(0x193a67f4a8a6d769) } + } + + pub fn seed(&mut self, seed: u64) { + println!("SEED {}", seed); + self.sm = SplitMix64::new(seed); + } + + #[inline] + pub fn next(&mut self) -> f32 { + self.sm.next_open01() as f32 + } +} // Copyright 2018 Developers of the Rand project. // @@ -126,7 +146,7 @@ impl RandGen { /// reference source code](http://xoshiro.di.unimi.it/splitmix64.c) by /// Sebastiano Vigna. For `next_u32`, a more efficient mixing function taken /// from [`dsiutils`](http://dsiutils.di.unimi.it/) is used. -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct SplitMix64(pub u64); const PHI: u64 = 0x9e3779b97f4a7c15;