rand_xoshiro/
xoshiro256plus.rs

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