rand_xoshiro/
xoroshiro64star.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 xoroshiro64* 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 `xoroshiro64star.c`
22/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64star.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 Xoroshiro64Star {
28    s0: u32,
29    s1: u32,
30}
31
32impl RngCore for Xoroshiro64Star {
33    #[inline]
34    fn next_u32(&mut self) -> u32 {
35        let r = self.s0.wrapping_mul(0x9E3779BB);
36        impl_xoroshiro_u32!(self);
37        r
38    }
39
40    #[inline]
41    fn next_u64(&mut self) -> u64 {
42        next_u64_via_u32(self)
43    }
44
45    #[inline]
46    fn fill_bytes(&mut self, dest: &mut [u8]) {
47        fill_bytes_via_next(self, dest);
48    }
49}
50
51impl SeedableRng for Xoroshiro64Star {
52    type Seed = [u8; 8];
53
54    /// Create a new `Xoroshiro64Star`.  If `seed` is entirely 0, it will be
55    /// mapped to a different seed.
56    fn from_seed(seed: [u8; 8]) -> Xoroshiro64Star {
57        deal_with_zero_seed!(seed, Self, 8);
58        let mut s = [0; 2];
59        read_u32_into(&seed, &mut s);
60
61        Xoroshiro64Star { s0: s[0], s1: s[1] }
62    }
63
64    /// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`.
65    fn seed_from_u64(seed: u64) -> Xoroshiro64Star {
66        from_splitmix!(seed)
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn reference() {
76        let mut rng = Xoroshiro64Star::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
77        // These values were produced with the reference implementation:
78        // http://xoshiro.di.unimi.it/xoshiro64star.c
79        let expected = [
80            2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673, 552743735,
81            1672597395, 1031040050, 2755315674,
82        ];
83        for &e in &expected {
84            assert_eq!(rng.next_u32(), e);
85        }
86    }
87
88    #[test]
89    fn zero_seed() {
90        let mut rng = Xoroshiro64Star::seed_from_u64(0);
91        assert_ne!(rng.next_u64(), 0);
92    }
93}