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::{RngCore, SeedableRng};
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15/// A xoshiro256** random number generator.
16///
17/// The xoshiro256** algorithm is not suitable for cryptographic purposes, but
18/// is very fast and has excellent statistical properties.
19///
20/// The algorithm used here is translated from [the `xoshiro256starstar.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoshiro256starstar.c) by
22/// David Blackman and Sebastiano Vigna.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub struct Xoshiro256StarStar {
26    s: [u64; 4],
27}
28
29impl Xoshiro256StarStar {
30    /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
31    ///
32    /// This can be used to generate 2^128 non-overlapping subsequences for
33    /// parallel computations.
34    ///
35    /// ```
36    /// use rand_xoshiro::rand_core::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    pub fn jump(&mut self) {
46        impl_jump!(
47            u64,
48            self,
49            [
50                0x180ec6d33cfd0aba,
51                0xd5a61266f0c9392c,
52                0xa9582618e03fc9aa,
53                0x39abdc4529b1661c
54            ]
55        );
56    }
57
58    /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
59    ///
60    /// This can be used to generate 2^64 starting points, from each of which
61    /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
62    /// distributed computations.
63    pub fn long_jump(&mut self) {
64        impl_jump!(
65            u64,
66            self,
67            [
68                0x76e15d3efefdcbbf,
69                0xc5004e441c522fb3,
70                0x77710069854ee241,
71                0x39109bb02acbe635
72            ]
73        );
74    }
75}
76
77impl SeedableRng for Xoshiro256StarStar {
78    type Seed = [u8; 32];
79
80    /// Create a new `Xoshiro256StarStar`.  If `seed` is entirely 0, it will be
81    /// mapped to a different seed.
82    #[inline]
83    fn from_seed(seed: [u8; 32]) -> Xoshiro256StarStar {
84        deal_with_zero_seed!(seed, Self);
85        let mut state = [0; 4];
86        read_u64_into(&seed, &mut state);
87        Xoshiro256StarStar { s: state }
88    }
89
90    /// Seed a `Xoshiro256StarStar` from a `u64` using `SplitMix64`.
91    fn seed_from_u64(seed: u64) -> Xoshiro256StarStar {
92        from_splitmix!(seed)
93    }
94}
95
96impl RngCore for Xoshiro256StarStar {
97    #[inline]
98    fn next_u32(&mut self) -> u32 {
99        // The lowest bits have some linear dependencies, so we use the
100        // upper bits instead.
101        (self.next_u64() >> 32) as u32
102    }
103
104    #[inline]
105    fn next_u64(&mut self) -> u64 {
106        let result_starstar = starstar_u64!(self.s[1]);
107        impl_xoshiro_u64!(self);
108        result_starstar
109    }
110
111    #[inline]
112    fn fill_bytes(&mut self, dest: &mut [u8]) {
113        fill_bytes_via_next(self, dest);
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn reference() {
123        let mut rng = Xoshiro256StarStar::from_seed([
124            1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0,
125            0, 0, 0,
126        ]);
127        // These values were produced with the reference implementation:
128        // http://xoshiro.di.unimi.it/xoshiro128starstar.c
129        let expected = [
130            11520,
131            0,
132            1509978240,
133            1215971899390074240,
134            1216172134540287360,
135            607988272756665600,
136            16172922978634559625,
137            8476171486693032832,
138            10595114339597558777,
139            2904607092377533576,
140        ];
141        for &e in &expected {
142            assert_eq!(rng.next_u64(), e);
143        }
144    }
145}