xoshiro/
xoshiro256plus.rs

1use rand_core::impls::fill_bytes_via_next;
2use rand_core::le::read_u64_into;
3use rand_core::{SeedableRng, RngCore, Error};
4
5/// A xoshiro256+ random number generator.
6///
7/// The xoshiro256+ algorithm is not suitable for cryptographic purposes, but
8/// is very fast and has good statistical properties, besides a low linear
9/// complexity in the lowest bits.
10///
11/// The algorithm used here is translated from [the `xoshiro256plus.c`
12/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plus.c) by
13/// David Blackman and Sebastiano Vigna.
14#[derive(Debug, Clone)]
15pub struct Xoshiro256Plus {
16    s: [u64; 4],
17}
18
19impl Xoshiro256Plus {
20    /// Seed a `Xoshiro256Plus` from a `u64` using `SplitMix64`.
21    pub fn from_seed_u64(seed: u64) -> Xoshiro256Plus {
22        from_splitmix!(seed)
23    }
24
25    /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
26    ///
27    /// This can be used to generate 2^128 non-overlapping subsequences for
28    /// parallel computations.
29    ///
30    /// ```
31    /// # extern crate rand;
32    /// # extern crate xoshiro;
33    /// # fn main() {
34    /// use rand::SeedableRng;
35    /// use xoshiro::Xoshiro256Plus;
36    ///
37    /// let rng1 = Xoshiro256Plus::from_seed_u64(0);
38    /// let mut rng2 = rng1.clone();
39    /// rng2.jump();
40    /// let mut rng3 = rng2.clone();
41    /// rng3.jump();
42    /// # }
43    /// ```
44    pub fn jump(&mut self) {
45        impl_jump!(u64, self, [
46            0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
47            0xa9582618e03fc9aa, 0x39abdc4529b1661c
48        ]);
49    }
50
51    /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
52    ///
53    /// This can be used to generate 2^64 starting points, from each of which
54    /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
55    /// distributed computations.
56    pub fn long_jump(&mut self) {
57        impl_jump!(u64, self, [
58            0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
59            0x77710069854ee241, 0x39109bb02acbe635
60        ]);
61    }
62}
63
64impl SeedableRng for Xoshiro256Plus {
65    type Seed = [u8; 32];
66
67    /// Create a new `Xoshiro256Plus`.  If `seed` is entirely 0, it will be
68    /// mapped to a different seed.
69    #[inline]
70    fn from_seed(seed: [u8; 32]) -> Xoshiro256Plus {
71        deal_with_zero_seed!(seed, Self);
72        let mut state = [0; 4];
73        read_u64_into(&seed, &mut state);
74        Xoshiro256Plus { s: state }
75    }
76}
77
78impl RngCore for Xoshiro256Plus {
79    #[inline]
80    fn next_u32(&mut self) -> u32 {
81        // The lowest bits have some linear dependencies, so we use the
82        // upper bits instead.
83        (self.next_u64() >> 32) as u32
84    }
85
86    #[inline]
87    fn next_u64(&mut self) -> u64 {
88        let result_plus = self.s[0].wrapping_add(self.s[3]);
89        impl_xoshiro_u64!(self);
90        result_plus
91    }
92
93    #[inline]
94    fn fill_bytes(&mut self, dest: &mut [u8]) {
95        fill_bytes_via_next(self, dest);
96    }
97
98    #[inline]
99    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
100        self.fill_bytes(dest);
101        Ok(())
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn reference() {
111        let mut rng = Xoshiro256Plus::from_seed(
112            [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
113             3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
114        // These values were produced with the reference implementation:
115        // http://xoshiro.di.unimi.it/xoshiro256plus.c
116        let expected = [
117            5, 211106232532999, 211106635186183, 9223759065350669058,
118            9250833439874351877, 13862484359527728515, 2346507365006083650,
119            1168864526675804870, 34095955243042024, 3466914240207415127,
120        ];
121        for &e in &expected {
122            assert_eq!(rng.next_u64(), e);
123        }
124    }
125}