rand_xoshiro/xoshiro256plusplus.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 xoshiro256++ random number generator.
16///
17/// The xoshiro256++ 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 `xoshiro256plusplus.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plusplus.c) by
22/// David Blackman and Sebastiano Vigna.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub struct Xoshiro256PlusPlus {
26 s: [u64; 4],
27}
28
29impl Xoshiro256PlusPlus {
30 /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
31 ///
32 /// This can be used to generate 2^128 non-overlapping subsequences for
33 /// parallel computations.
34 ///
35 /// ```
36 /// use rand_xoshiro::rand_core::SeedableRng;
37 /// use rand_xoshiro::Xoshiro256PlusPlus;
38 ///
39 /// let rng1 = Xoshiro256PlusPlus::seed_from_u64(0);
40 /// let mut rng2 = rng1.clone();
41 /// rng2.jump();
42 /// let mut rng3 = rng2.clone();
43 /// rng3.jump();
44 /// ```
45 pub fn jump(&mut self) {
46 impl_jump!(
47 u64,
48 self,
49 [
50 0x180ec6d33cfd0aba,
51 0xd5a61266f0c9392c,
52 0xa9582618e03fc9aa,
53 0x39abdc4529b1661c
54 ]
55 );
56 }
57
58 /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
59 ///
60 /// This can be used to generate 2^64 starting points, from each of which
61 /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
62 /// distributed computations.
63 pub fn long_jump(&mut self) {
64 impl_jump!(
65 u64,
66 self,
67 [
68 0x76e15d3efefdcbbf,
69 0xc5004e441c522fb3,
70 0x77710069854ee241,
71 0x39109bb02acbe635
72 ]
73 );
74 }
75}
76
77impl SeedableRng for Xoshiro256PlusPlus {
78 type Seed = [u8; 32];
79
80 /// Create a new `Xoshiro256PlusPlus`. If `seed` is entirely 0, it will be
81 /// mapped to a different seed.
82 #[inline]
83 fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus {
84 deal_with_zero_seed!(seed, Self);
85 let mut state = [0; 4];
86 read_u64_into(&seed, &mut state);
87 Xoshiro256PlusPlus { s: state }
88 }
89
90 /// Seed a `Xoshiro256PlusPlus` from a `u64` using `SplitMix64`.
91 fn seed_from_u64(seed: u64) -> Xoshiro256PlusPlus {
92 from_splitmix!(seed)
93 }
94}
95
96impl RngCore for Xoshiro256PlusPlus {
97 #[inline]
98 fn next_u32(&mut self) -> u32 {
99 // The lowest bits have some linear dependencies, so we use the
100 // upper bits instead.
101 (self.next_u64() >> 32) as u32
102 }
103
104 #[inline]
105 fn next_u64(&mut self) -> u64 {
106 let result_plusplus = plusplus_u64!(self.s[0], self.s[3], 23);
107 impl_xoshiro_u64!(self);
108 result_plusplus
109 }
110
111 #[inline]
112 fn fill_bytes(&mut self, dest: &mut [u8]) {
113 fill_bytes_via_next(self, dest);
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn reference() {
123 let mut rng = Xoshiro256PlusPlus::from_seed([
124 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0,
125 0, 0, 0,
126 ]);
127 // These values were produced with the reference implementation:
128 // http://xoshiro.di.unimi.it/xoshiro256plusplus.c
129 let expected = [
130 41943041,
131 58720359,
132 3588806011781223,
133 3591011842654386,
134 9228616714210784205,
135 9973669472204895162,
136 14011001112246962877,
137 12406186145184390807,
138 15849039046786891736,
139 10450023813501588000,
140 ];
141 for &e in &expected {
142 assert_eq!(rng.next_u64(), e);
143 }
144 }
145}