Skip to main content

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 core::convert::Infallible;
10use rand_core::{SeedableRng, TryRng, utils};
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14/// A xoroshiro64* random number generator.
15///
16/// The xoroshiro64* 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 `xoroshiro64star.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64star.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 Xoroshiro64Star {
27    s0: u32,
28    s1: u32,
29}
30
31impl TryRng for Xoroshiro64Star {
32    type Error = Infallible;
33
34    #[inline]
35    fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
36        let r = self.s0.wrapping_mul(0x9E3779BB);
37        impl_xoroshiro_u32!(self);
38        Ok(r)
39    }
40
41    #[inline]
42    fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
43        utils::next_u64_via_u32(self)
44    }
45
46    #[inline]
47    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
48        utils::fill_bytes_via_next_word(dest, || self.try_next_u32())
49    }
50}
51
52impl_state_pair!(Xoroshiro64Star, u32);
53
54impl SeedableRng for Xoroshiro64Star {
55    type Seed = [u8; 8];
56
57    /// Create a new `Xoroshiro64Star`.  If `seed` is entirely 0, it will be
58    /// mapped to a different seed.
59    fn from_seed(seed: [u8; 8]) -> Xoroshiro64Star {
60        let s: [_; 2] = utils::read_words(crate::common::zero_seed_fallback(&seed));
61
62        Xoroshiro64Star { s0: s[0], s1: s[1] }
63    }
64
65    /// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`.
66    fn seed_from_u64(seed: u64) -> Xoroshiro64Star {
67        from_splitmix!(seed)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use rand_core::Rng;
75
76    #[test]
77    fn reference() {
78        let mut rng = Xoroshiro64Star::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
79        // These values were produced with the reference implementation:
80        // http://xoshiro.di.unimi.it/xoshiro64star.c
81        let expected = [
82            2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673, 552743735,
83            1672597395, 1031040050, 2755315674,
84        ];
85        for &e in &expected {
86            assert_eq!(rng.next_u32(), e);
87        }
88    }
89
90    #[test]
91    fn zero_seed() {
92        let mut rng = Xoroshiro64Star::seed_from_u64(0);
93        assert_ne!(rng.next_u64(), 0);
94    }
95
96    #[test]
97    fn zero_seed_maps_to_seed_from_u64_zero() {
98        let from_zero = Xoroshiro64Star::from_seed([0u8; 8]);
99        let from_sm0 = Xoroshiro64Star::seed_from_u64(0);
100        assert_eq!(from_zero, from_sm0);
101    }
102
103    #[test]
104    fn state_roundtrip() {
105        let rng = Xoroshiro64Star::seed_from_u64(42);
106        let clone = Xoroshiro64Star::from_seed(rng.state());
107        assert_eq!(clone, rng);
108    }
109}