rand_xoshiro/xoshiro128starstar.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::{next_u64_via_u32, fill_bytes_via_next};
10use rand_core::le::read_u32_into;
11use rand_core::{SeedableRng, RngCore, Error};
12
13/// A xoshiro128** random number generator.
14///
15/// The xoshiro128** algorithm is not suitable for cryptographic purposes, but
16/// is very fast and has excellent statistical properties.
17///
18/// The algorithm used here is translated from [the `xoshiro128starstar.c`
19/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
20/// David Blackman and Sebastiano Vigna.
21#[derive(Debug, Clone)]
22pub struct Xoshiro128StarStar {
23 s: [u32; 4],
24}
25
26impl Xoshiro128StarStar {
27 /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
28 ///
29 /// This can be used to generate 2^64 non-overlapping subsequences for
30 /// parallel computations.
31 ///
32 /// ```
33 /// # extern crate rand;
34 /// # extern crate rand_xoshiro;
35 /// # fn main() {
36 /// use rand::SeedableRng;
37 /// use rand_xoshiro::Xoroshiro128StarStar;
38 ///
39 /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
40 /// let mut rng2 = rng1.clone();
41 /// rng2.jump();
42 /// let mut rng3 = rng2.clone();
43 /// rng3.jump();
44 /// # }
45 /// ```
46 pub fn jump(&mut self) {
47 impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
48 }
49}
50
51impl SeedableRng for Xoshiro128StarStar {
52 type Seed = [u8; 16];
53
54 /// Create a new `Xoshiro128StarStar`. If `seed` is entirely 0, it will be
55 /// mapped to a different seed.
56 #[inline]
57 fn from_seed(seed: [u8; 16]) -> Xoshiro128StarStar {
58 deal_with_zero_seed!(seed, Self);
59 let mut state = [0; 4];
60 read_u32_into(&seed, &mut state);
61 Xoshiro128StarStar { s: state }
62 }
63
64 /// Seed a `Xoshiro128StarStar` from a `u64` using `SplitMix64`.
65 fn seed_from_u64(seed: u64) -> Xoshiro128StarStar {
66 from_splitmix!(seed)
67 }
68}
69
70impl RngCore for Xoshiro128StarStar {
71 #[inline]
72 fn next_u32(&mut self) -> u32 {
73 let result_starstar = starstar_u64!(self.s[0]);
74 impl_xoshiro_u32!(self);
75 result_starstar
76 }
77
78 #[inline]
79 fn next_u64(&mut self) -> u64 {
80 next_u64_via_u32(self)
81 }
82
83 #[inline]
84 fn fill_bytes(&mut self, dest: &mut [u8]) {
85 fill_bytes_via_next(self, dest);
86 }
87
88 #[inline]
89 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
90 self.fill_bytes(dest);
91 Ok(())
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn reference() {
101 let mut rng = Xoshiro128StarStar::from_seed(
102 [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
103 // These values were produced with the reference implementation:
104 // http://xoshiro.di.unimi.it/xoshiro128starstar.c
105 let expected = [
106 5760, 40320, 70819200, 3297914139, 2480851620, 1792823698,
107 4118739149, 1251203317, 1581886583, 1721184582,
108 ];
109 for &e in &expected {
110 assert_eq!(rng.next_u32(), e);
111 }
112 }
113}