rand_xoshiro/
xoroshiro64starstar.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, next_u64_via_u32};
10use rand_core::le::read_u32_into;
11use rand_core::{RngCore, SeedableRng};
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15/// A xoroshiro64** random number generator.
16///
17/// The xoshiro64** 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 `xoroshiro64starstar.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64starstar.c) by
22/// David Blackman and Sebastiano Vigna.
23#[allow(missing_copy_implementations)]
24#[derive(Debug, Clone, PartialEq, Eq)]
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26pub struct Xoroshiro64StarStar {
27    s0: u32,
28    s1: u32,
29}
30
31impl RngCore for Xoroshiro64StarStar {
32    #[inline]
33    fn next_u32(&mut self) -> u32 {
34        let r = starstar_u32!(self.s0);
35        impl_xoroshiro_u32!(self);
36        r
37    }
38
39    #[inline]
40    fn next_u64(&mut self) -> u64 {
41        next_u64_via_u32(self)
42    }
43
44    #[inline]
45    fn fill_bytes(&mut self, dest: &mut [u8]) {
46        fill_bytes_via_next(self, dest);
47    }
48}
49
50impl SeedableRng for Xoroshiro64StarStar {
51    type Seed = [u8; 8];
52
53    /// Create a new `Xoroshiro64StarStar`.  If `seed` is entirely 0, it will be
54    /// mapped to a different seed.
55    fn from_seed(seed: [u8; 8]) -> Xoroshiro64StarStar {
56        deal_with_zero_seed!(seed, Self, 8);
57        let mut s = [0; 2];
58        read_u32_into(&seed, &mut s);
59
60        Xoroshiro64StarStar { s0: s[0], s1: s[1] }
61    }
62
63    /// Seed a `Xoroshiro64StarStar` from a `u64` using `SplitMix64`.
64    fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar {
65        from_splitmix!(seed)
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn reference() {
75        let mut rng = Xoroshiro64StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
76        // These values were produced with the reference implementation:
77        // http://xoshiro.di.unimi.it/xoshiro64starstar.c
78        let expected = [
79            3802928447, 813792938, 1618621494, 2955957307, 3252880261, 1129983909, 2539651700,
80            1327610908, 1757650787, 2763843748,
81        ];
82        for &e in &expected {
83            assert_eq!(rng.next_u32(), e);
84        }
85    }
86
87    #[test]
88    fn zero_seed() {
89        let mut rng = Xoroshiro64StarStar::seed_from_u64(0);
90        assert_ne!(rng.next_u64(), 0);
91    }
92}