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
9#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
10use rand_core::impls::fill_bytes_via_next;
11use rand_core::le::read_u64_into;
12use rand_core::{SeedableRng, RngCore, Error};
13
14/// A xoshiro256+ random number generator.
15///
16/// The xoshiro256+ 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 `xoshiro256plus.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plus.c) by
22/// David Blackman and Sebastiano Vigna.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
25pub struct Xoshiro256Plus {
26 s: [u64; 4],
27}
28
29impl Xoshiro256Plus {
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::Xoshiro256Plus;
38 ///
39 /// let rng1 = Xoshiro256Plus::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!(u64, self, [
47 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
48 0xa9582618e03fc9aa, 0x39abdc4529b1661c
49 ]);
50 }
51
52 /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
53 ///
54 /// This can be used to generate 2^64 starting points, from each of which
55 /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
56 /// distributed computations.
57 pub fn long_jump(&mut self) {
58 impl_jump!(u64, self, [
59 0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
60 0x77710069854ee241, 0x39109bb02acbe635
61 ]);
62 }
63}
64
65impl SeedableRng for Xoshiro256Plus {
66 type Seed = [u8; 32];
67
68 /// Create a new `Xoshiro256Plus`. If `seed` is entirely 0, it will be
69 /// mapped to a different seed.
70 #[inline]
71 fn from_seed(seed: [u8; 32]) -> Xoshiro256Plus {
72 deal_with_zero_seed!(seed, Self);
73 let mut state: [u64; 4] = [0; 4];
74 read_u64_into(&seed, &mut state);
75 Xoshiro256Plus { s: state }
76 }
77
78 /// Seed a `Xoshiro256Plus` from a `u64` using `SplitMix64`.
79 fn seed_from_u64(seed: u64) -> Xoshiro256Plus {
80 from_splitmix!(seed)
81 }
82}
83
84impl RngCore for Xoshiro256Plus {
85 #[inline]
86 fn next_u32(&mut self) -> u32 {
87 // The lowest bits have some linear dependencies, so we use the
88 // upper bits instead.
89 (self.next_u64() >> 32) as u32
90 }
91
92 #[inline]
93 fn next_u64(&mut self) -> u64 {
94 let result_plus = self.s[0].wrapping_add(self.s[3]);
95 impl_xoshiro_u64!(self);
96 result_plus
97 }
98
99 #[inline]
100 fn fill_bytes(&mut self, dest: &mut [u8]) {
101 fill_bytes_via_next(self, dest);
102 }
103
104 #[inline]
105 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
106 self.fill_bytes(dest);
107 Ok(())
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn reference() {
117 let mut rng = Xoshiro256Plus::from_seed(
118 [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
119 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
120 // These values were produced with the reference implementation:
121 // http://xoshiro.di.unimi.it/xoshiro256plus.c
122 let expected = [
123 5, 211106232532999, 211106635186183, 9223759065350669058,
124 9250833439874351877, 13862484359527728515, 2346507365006083650,
125 1168864526675804870, 34095955243042024, 3466914240207415127,
126 ];
127 for &e in &expected {
128 assert_eq!(rng.next_u64(), e);
129 }
130 }
131}
132