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