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