rand_xoshiro/
xoshiro256starstar.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 excellent statistical properties.
17///
18/// The algorithm used here is translated from [the `xoshiro256starstar.c`
19/// reference source code](http://xoshiro.di.unimi.it/xoshiro256starstar.c) by
20/// David Blackman and Sebastiano Vigna.
21#[derive(Debug, Clone)]
22pub struct Xoshiro256StarStar {
23    s: [u64; 4],
24}
25
26impl Xoshiro256StarStar {
27    /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
28    ///
29    /// This can be used to generate 2^128 non-overlapping subsequences for
30    /// parallel computations.
31    ///
32    /// ```
33    /// # extern crate rand;
34    /// # extern crate rand_xoshiro;
35    /// # fn main() {
36    /// use rand::SeedableRng;
37    /// use rand_xoshiro::Xoshiro256StarStar;
38    ///
39    /// let rng1 = Xoshiro256StarStar::seed_from_u64(0);
40    /// let mut rng2 = rng1.clone();
41    /// rng2.jump();
42    /// let mut rng3 = rng2.clone();
43    /// rng3.jump();
44    /// # }
45    /// ```
46    pub fn jump(&mut self) {
47        impl_jump!(u64, self, [
48            0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
49            0xa9582618e03fc9aa, 0x39abdc4529b1661c
50        ]);
51    }
52
53    /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
54    ///
55    /// This can be used to generate 2^64 starting points, from each of which
56    /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
57    /// distributed computations.
58    pub fn long_jump(&mut self) {
59        impl_jump!(u64, self, [
60            0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
61            0x77710069854ee241, 0x39109bb02acbe635
62        ]);
63    }
64}
65
66impl SeedableRng for Xoshiro256StarStar {
67    type Seed = [u8; 32];
68
69    /// Create a new `Xoshiro256StarStar`.  If `seed` is entirely 0, it will be
70    /// mapped to a different seed.
71    #[inline]
72    fn from_seed(seed: [u8; 32]) -> Xoshiro256StarStar {
73        deal_with_zero_seed!(seed, Self);
74        let mut state = [0; 4];
75        read_u64_into(&seed, &mut state);
76        Xoshiro256StarStar { s: state }
77    }
78
79    /// Seed a `Xoshiro256StarStar` from a `u64` using `SplitMix64`.
80    fn seed_from_u64(seed: u64) -> Xoshiro256StarStar {
81        from_splitmix!(seed)
82    }
83}
84
85impl RngCore for Xoshiro256StarStar {
86    #[inline]
87    fn next_u32(&mut self) -> u32 {
88        self.next_u64() as u32
89    }
90
91    #[inline]
92    fn next_u64(&mut self) -> u64 {
93        let result_starstar = starstar_u64!(self.s[1]);
94        impl_xoshiro_u64!(self);
95        result_starstar
96    }
97
98    #[inline]
99    fn fill_bytes(&mut self, dest: &mut [u8]) {
100        fill_bytes_via_next(self, dest);
101    }
102
103    #[inline]
104    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
105        self.fill_bytes(dest);
106        Ok(())
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn reference() {
116        let mut rng = Xoshiro256StarStar::from_seed(
117            [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
118             3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
119        // These values were produced with the reference implementation:
120        // http://xoshiro.di.unimi.it/xoshiro128starstar.c
121        let expected = [
122            11520, 0, 1509978240, 1215971899390074240, 1216172134540287360,
123            607988272756665600, 16172922978634559625, 8476171486693032832,
124            10595114339597558777, 2904607092377533576,
125        ];
126        for &e in &expected {
127            assert_eq!(rng.next_u64(), e);
128        }
129    }
130}