rand_xoshiro/xoroshiro128starstar.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;
10use rand_core::le::read_u64_into;
11use rand_core::impls::fill_bytes_via_next;
12use rand_core::{RngCore, SeedableRng};
13
14/// A xoroshiro128** random number generator.
15///
16/// The xoroshiro128** algorithm is not suitable for cryptographic purposes, but
17/// is very fast and has excellent statistical properties.
18///
19/// The algorithm used here is translated from [the `xoroshiro128starstar.c`
20/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128starstar.c) by
21/// David Blackman and Sebastiano Vigna.
22#[allow(missing_copy_implementations)]
23#[derive(Debug, Clone)]
24pub struct Xoroshiro128StarStar {
25 s0: u64,
26 s1: u64,
27}
28
29impl Xoroshiro128StarStar {
30 /// Jump forward, equivalently to 2^64 calls to `next_u64()`.
31 ///
32 /// This can be used to generate 2^64 non-overlapping subsequences for
33 /// parallel computations.
34 ///
35 /// ```
36 /// # extern crate rand;
37 /// # extern crate rand_xoshiro;
38 /// # fn main() {
39 /// use rand::SeedableRng;
40 /// use rand_xoshiro::Xoroshiro128StarStar;
41 ///
42 /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
43 /// let mut rng2 = rng1.clone();
44 /// rng2.jump();
45 /// let mut rng3 = rng2.clone();
46 /// rng3.jump();
47 /// # }
48 /// ```
49 pub fn jump(&mut self) {
50 impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
51 }
52
53 /// Jump forward, equivalently to 2^96 calls to `next_u64()`.
54 ///
55 /// This can be used to generate 2^32 starting points, from each of which
56 /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
57 /// distributed computations.
58 pub fn long_jump(&mut self) {
59 impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
60 }
61}
62
63impl RngCore for Xoroshiro128StarStar {
64 #[inline]
65 fn next_u32(&mut self) -> u32 {
66 self.next_u64() as u32
67 }
68
69 #[inline]
70 fn next_u64(&mut self) -> u64 {
71 let r = starstar_u64!(self.s0);
72 impl_xoroshiro_u64!(self);
73 r
74 }
75
76 #[inline]
77 fn fill_bytes(&mut self, dest: &mut [u8]) {
78 fill_bytes_via_next(self, dest);
79 }
80
81 #[inline]
82 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
83 self.fill_bytes(dest);
84 Ok(())
85 }
86}
87
88impl SeedableRng for Xoroshiro128StarStar {
89 type Seed = [u8; 16];
90
91 /// Create a new `Xoroshiro128StarStar`. If `seed` is entirely 0, it will be
92 /// mapped to a different seed.
93 fn from_seed(seed: [u8; 16]) -> Xoroshiro128StarStar {
94 deal_with_zero_seed!(seed, Self);
95 let mut s = [0; 2];
96 read_u64_into(&seed, &mut s);
97
98 Xoroshiro128StarStar {
99 s0: s[0],
100 s1: s[1],
101 }
102 }
103
104 /// Seed a `Xoroshiro128StarStar` from a `u64` using `SplitMix64`.
105 fn seed_from_u64(seed: u64) -> Xoroshiro128StarStar {
106 from_splitmix!(seed)
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn reference() {
116 let mut rng = Xoroshiro128StarStar::from_seed(
117 [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
118 // These values were produced with the reference implementation:
119 // http://xoshiro.di.unimi.it/xoshiro128starstar.c
120 let expected = [
121 5760, 97769243520, 9706862127477703552, 9223447511460779954,
122 8358291023205304566, 15695619998649302768, 8517900938696309774,
123 16586480348202605369, 6959129367028440372, 16822147227405758281,
124 ];
125 for &e in &expected {
126 assert_eq!(rng.next_u64(), e);
127 }
128 }
129}