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::{next_u64_via_u32, fill_bytes_via_next};
11use rand_core::le::read_u32_into;
12use rand_core::{SeedableRng, RngCore, Error};
13
14/// A xoshiro128++ random number generator.
15///
16/// The xoshiro128++ 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 `xoshiro128plusplus.c`
20/// reference source code](http://xoshiro.di.unimi.it/xoshiro128plusplus.c) by
21/// David Blackman and Sebastiano Vigna.
22#[derive(Debug, Clone, PartialEq, Eq)]
23#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
24pub struct Xoshiro128PlusPlus {
25 s: [u32; 4],
26}
27
28impl Xoshiro128PlusPlus {
29 /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
30 ///
31 /// This can be used to generate 2^64 non-overlapping subsequences for
32 /// parallel computations.
33 ///
34 /// ```
35 /// use rand_xoshiro::rand_core::SeedableRng;
36 /// use rand_xoshiro::Xoroshiro128PlusPlus;
37 ///
38 /// let rng1 = Xoroshiro128PlusPlus::seed_from_u64(0);
39 /// let mut rng2 = rng1.clone();
40 /// rng2.jump();
41 /// let mut rng3 = rng2.clone();
42 /// rng3.jump();
43 /// ```
44 pub fn jump(&mut self) {
45 impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
46 }
47
48 /// Jump forward, equivalently to 2^96 calls to `next_u32()`.
49 ///
50 /// This can be used to generate 2^32 starting points, from each of which
51 /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
52 /// distributed computations.
53 pub fn long_jump(&mut self) {
54 impl_jump!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]);
55 }
56}
57
58impl SeedableRng for Xoshiro128PlusPlus {
59 type Seed = [u8; 16];
60
61 /// Create a new `Xoshiro128PlusPlus`. If `seed` is entirely 0, it will be
62 /// mapped to a different seed.
63 #[inline]
64 fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus {
65 deal_with_zero_seed!(seed, Self);
66 let mut state: [u32; 4] = [0; 4];
67 read_u32_into(&seed, &mut state);
68 Xoshiro128PlusPlus { s: state }
69 }
70
71 /// Seed a `Xoshiro128PlusPlus` from a `u64` using `SplitMix64`.
72 fn seed_from_u64(seed: u64) -> Xoshiro128PlusPlus {
73 from_splitmix!(seed)
74 }
75}
76
77impl RngCore for Xoshiro128PlusPlus {
78 #[inline]
79 fn next_u32(&mut self) -> u32 {
80 let result_starstar: u32 = plusplus_u32!(self.s[0], self.s[3]);
81 impl_xoshiro_u32!(self);
82 result_starstar
83 }
84
85 #[inline]
86 fn next_u64(&mut self) -> u64 {
87 next_u64_via_u32(self)
88 }
89
90 #[inline]
91 fn fill_bytes(&mut self, dest: &mut [u8]) {
92 fill_bytes_via_next(self, dest);
93 }
94
95 #[inline]
96 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
97 self.fill_bytes(dest);
98 Ok(())
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn reference() {
108 let mut rng = Xoshiro128PlusPlus::from_seed(
109 [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
110 // These values were produced with the reference implementation:
111 // http://xoshiro.di.unimi.it/xoshiro128plusplus.c
112 let expected = [
113 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768,
114 3867114732, 1355841295, 495546011, 621204420,
115 ];
116 for &e in &expected {
117 assert_eq!(rng.next_u32(), e);
118 }
119 }
120}
121