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 good statistical properties, besides a low linear
18/// complexity in the lowest bits.
19///
20/// The algorithm used here is translated from [the `xoshiro128starstar.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
22/// David Blackman and Sebastiano Vigna.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
25pub struct Xoshiro128Plus {
26 s: [u32; 4],
27}
28
29impl Xoshiro128Plus {
30 /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
31 ///
32 /// This can be used to generate 2^64 non-overlapping subsequences for
33 /// parallel computations.
34 ///
35 /// ```
36 /// use rand_xoshiro::rand_core::SeedableRng;
37 /// use rand_xoshiro::Xoroshiro128StarStar;
38 ///
39 /// let rng1 = Xoroshiro128StarStar::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!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
47 }
48}
49
50impl SeedableRng for Xoshiro128Plus {
51 type Seed = [u8; 16];
52
53 /// Create a new `Xoshiro128Plus`. If `seed` is entirely 0, it will be
54 /// mapped to a different seed.
55 #[inline]
56 fn from_seed(seed: [u8; 16]) -> Xoshiro128Plus {
57 deal_with_zero_seed!(seed, Self);
58 let mut state: [u32; 4] = [0; 4];
59 read_u32_into(&seed, &mut state);
60 Xoshiro128Plus { s: state }
61 }
62
63 /// Seed a `Xoshiro128Plus` from a `u64` using `SplitMix64`.
64 fn seed_from_u64(seed: u64) -> Xoshiro128Plus {
65 from_splitmix!(seed)
66 }
67}
68
69impl RngCore for Xoshiro128Plus {
70 #[inline]
71 fn next_u32(&mut self) -> u32 {
72 let result_plus: u32 = self.s[0].wrapping_add(self.s[3]);
73 impl_xoshiro_u32!(self);
74 result_plus
75 }
76
77 #[inline]
78 fn next_u64(&mut self) -> u64 {
79 next_u64_via_u32(self)
80 }
81
82 #[inline]
83 fn fill_bytes(&mut self, dest: &mut [u8]) {
84 fill_bytes_via_next(self, dest);
85 }
86
87 #[inline]
88 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
89 self.fill_bytes(dest);
90 Ok(())
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn reference() {
100 let mut rng = Xoshiro128Plus::from_seed(
101 [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
102 // These values were produced with the reference implementation:
103 // http://xoshiro.di.unimi.it/xoshiro128plus.c
104 let expected = [
105 5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097,
106 4110231701, 399823256, 2144435200,
107 ];
108 for &e in &expected {
109 assert_eq!(rng.next_u32(), e);
110 }
111 }
112}
113