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