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 byteorder::{ByteOrder, LittleEndian};
10use rand_core;
11use rand_core::le::read_u32_into;
12use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
13use rand_core::{RngCore, SeedableRng};
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)]
25pub struct Xoroshiro64StarStar {
26    s0: u32,
27    s1: u32,
28}
29
30impl RngCore for Xoroshiro64StarStar {
31    #[inline]
32    fn next_u32(&mut self) -> u32 {
33        let r = starstar_u32!(self.s0);
34        impl_xoroshiro_u32!(self);
35        r
36    }
37
38    #[inline]
39    fn next_u64(&mut self) -> u64 {
40        next_u64_via_u32(self)
41    }
42
43    #[inline]
44    fn fill_bytes(&mut self, dest: &mut [u8]) {
45        fill_bytes_via_next(self, dest);
46    }
47
48    #[inline]
49    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
50        self.fill_bytes(dest);
51        Ok(())
52    }
53}
54
55impl SeedableRng for Xoroshiro64StarStar {
56    type Seed = [u8; 8];
57
58    /// Create a new `Xoroshiro64StarStar`.  If `seed` is entirely 0, it will be
59    /// mapped to a different seed.
60    fn from_seed(seed: [u8; 8]) -> Xoroshiro64StarStar {
61        deal_with_zero_seed!(seed, Self);
62        let mut s = [0; 2];
63        read_u32_into(&seed, &mut s);
64
65        Xoroshiro64StarStar {
66            s0: s[0],
67            s1: s[1],
68        }
69    }
70
71    /// Seed a `Xoroshiro64StarStar` from a `u64`.
72    fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar {
73        let mut s = [0; 8];
74        LittleEndian::write_u64(&mut s, seed);
75        Xoroshiro64StarStar::from_seed(s)
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn reference() {
85        let mut rng = Xoroshiro64StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
86        // These values were produced with the reference implementation:
87        // http://xoshiro.di.unimi.it/xoshiro64starstar.c
88        let expected = [
89            3802928447, 813792938, 1618621494, 2955957307, 3252880261,
90            1129983909, 2539651700, 1327610908, 1757650787, 2763843748,
91        ];
92        for &e in &expected {
93            assert_eq!(rng.next_u32(), e);
94        }
95    }
96}