1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Simple pseudorandom number generator
//!
//! license: Public Domain
//!
//! [https://github.com/BillyDM/Fast-DSP-Approximations/blob/main/rng_and_noise.md](https://github.com/BillyDM/Fast-DSP-Approximations/blob/main/rng_and_noise.md)
/// A simple, fast and repeatable pseudo-random number generator
#[derive(Clone, Copy)]
pub struct XOrShift32Rng {
fpd: u32,
}
impl Default for XOrShift32Rng {
fn default() -> XOrShift32Rng {
XOrShift32Rng { fpd: 17 }
}
}
impl XOrShift32Rng {
/// Create a new Self with the given `seed`. The RNG gives the same sequence
/// of numbers for the same seed. The seed cannot be zero. If seed is set to
/// zero it will be remapped to 17.
pub fn new(mut seed: u32) -> XOrShift32Rng {
// seed cannot be zero
if seed == 0 {
seed = 17;
}
XOrShift32Rng { fpd: seed }
}
/// Returns a pseudo random u32. This is the native format of the RNG.
#[inline]
pub fn gen_u32(&mut self) -> u32 {
self.fpd ^= self.fpd << 13;
self.fpd ^= self.fpd >> 17;
self.fpd ^= self.fpd << 5;
self.fpd
}
/// Convenience function to convert [`XOrShift32Rng::gen_u32`] to an f32 in the range 0.0..=1.0
#[inline]
pub fn gen_f32(&mut self) -> f32 {
self.gen_u32() as f32 / std::u32::MAX as f32
}
/// Convenience function to convert [`XOrShift32Rng::gen_u32`] to an f32 in the range 0.0..=1.0
#[inline]
pub fn gen_f64(&mut self) -> f64 {
self.gen_u32() as f64 / std::u32::MAX as f64
}
}