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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! A tiny PRNG.
//!
//! More specifically, it implements a single GOOD PRNG, which is
//! currently a permuted congruential generator.  It has two
//! implementations, one that returns `u32` and one that returns
//! `u64`.  Someday I'll add functions that return floats, signed
//! integers, and integer ranges.  And that's probably about it.  What
//! more do you need?
//!
//! For more info on PCG generators, see http://www.pcg-random.org/
//!
//! This was designed as a minimalist utility for video games.  No
//! promises are made about its quality, and if you use it for
//! cryptography you will get what you deserve.
//!
//! Works with `#![no_std]`, has no global state, and is generally
//! neato.


#![no_std]

/// A PRNG producing a 32-bit output.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rand32 {
    state: u64,
    inc: u64,
}

impl Rand32 {
    pub fn new(seed: u64) -> Self {
        // TODO someday: `inc` is a parameter that can be tinkered with,
        // but has constraints.
        // The value used here is from the PCG minimal C implementation: http://www.pcg-random.org/download.html
        let mut rng = Self {
            state: 0,
            inc: (0xda3e39cb94b95bdb << 1) | 1,
        };
        // This initialization song-and-dance is a little odd; verify.
        let _ = rng.rand();
        rng.state += seed;
        let _ = rng.rand();
        rng
    }
    
    pub fn rand(&mut self) -> u32 {
        let oldstate: u64 = self.state;
        self.state = oldstate * 6364136223846793005 + self.inc;
        let xorshifted: u32 = (((oldstate >> 18) ^ oldstate) >> 27) as u32;
        let rot: u32 = (oldstate >> 59) as u32;
        (xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))
    }
}


/// A PRNG producing a 64-bit output.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rand64 {
    state: u128,
    inc: u128,
}

impl Rand64 {
    pub fn new(seed: u128) -> Self {
        // TODO someday: `inc` is a parameter that can be tinkered with,
        // but has constraints.
        // The value used here is from the PCG minimal C implementation: http://www.pcg-random.org/download.html
        let mut rng = Self {
            state: 0,
            inc: (63641362238467930051442695040888963407 << 1) | 1,
        };
        // This initialization song-and-dance is a little odd; verify.
        let _ = rng.rand();
        rng.state += seed;
        let _ = rng.rand();
        rng
    }
    
    pub fn rand(&mut self) -> u64 {
        let oldstate: u128 = self.state;
        self.state = oldstate * 25492979953554139244865540595714422341 + self.inc;
        let xorshifted: u64 = (((oldstate >> 29) ^ oldstate) >> 58) as u64;
        let rot: u64 = (oldstate >> 112) as u64;
        (xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))
    }
}


#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}