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 | use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; |
10 | use rand_core::le::read_u32_into; |
11 | use rand_core::{RngCore, SeedableRng}; |
12 | #[cfg (feature = "serde" )] |
13 | use serde::{Deserialize, Serialize}; |
14 | |
15 | /// A xoshiro128+ random number generator. |
16 | /// |
17 | /// The xoshiro128+ algorithm is not suitable for cryptographic purposes, but |
18 | /// is very fast and has good statistical properties, besides a low linear |
19 | /// complexity in the lowest bits. |
20 | /// |
21 | /// The algorithm used here is translated from [the `xoshiro128starstar.c` |
22 | /// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by |
23 | /// David Blackman and Sebastiano Vigna. |
24 | #[derive (Debug, Clone, PartialEq, Eq)] |
25 | #[cfg_attr (feature = "serde" , derive(Serialize, Deserialize))] |
26 | pub struct Xoshiro128Plus { |
27 | s: [u32; 4], |
28 | } |
29 | |
30 | impl Xoshiro128Plus { |
31 | /// Jump forward, equivalently to 2^64 calls to `next_u32()`. |
32 | /// |
33 | /// This can be used to generate 2^64 non-overlapping subsequences for |
34 | /// parallel computations. |
35 | /// |
36 | /// ``` |
37 | /// use rand_xoshiro::rand_core::SeedableRng; |
38 | /// use rand_xoshiro::Xoroshiro128StarStar; |
39 | /// |
40 | /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0); |
41 | /// let mut rng2 = rng1.clone(); |
42 | /// rng2.jump(); |
43 | /// let mut rng3 = rng2.clone(); |
44 | /// rng3.jump(); |
45 | /// ``` |
46 | pub fn jump(&mut self) { |
47 | impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]); |
48 | } |
49 | |
50 | /// Jump forward, equivalently to 2^96 calls to `next_u32()`. |
51 | /// |
52 | /// This can be used to generate 2^32 starting points, from each of which |
53 | /// `jump()` will generate 2^32 non-overlapping subsequences for parallel |
54 | /// distributed computations. |
55 | pub fn long_jump(&mut self) { |
56 | impl_jump!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]); |
57 | } |
58 | } |
59 | |
60 | impl SeedableRng for Xoshiro128Plus { |
61 | type Seed = [u8; 16]; |
62 | |
63 | /// Create a new `Xoshiro128Plus`. If `seed` is entirely 0, it will be |
64 | /// mapped to a different seed. |
65 | #[inline ] |
66 | fn from_seed(seed: [u8; 16]) -> Xoshiro128Plus { |
67 | deal_with_zero_seed!(seed, Self, 16); |
68 | let mut state: [u32; 4] = [0; 4]; |
69 | read_u32_into(&seed, &mut state); |
70 | Xoshiro128Plus { s: state } |
71 | } |
72 | |
73 | /// Seed a `Xoshiro128Plus` from a `u64` using `SplitMix64`. |
74 | fn seed_from_u64(seed: u64) -> Xoshiro128Plus { |
75 | from_splitmix!(seed) |
76 | } |
77 | } |
78 | |
79 | impl RngCore for Xoshiro128Plus { |
80 | #[inline ] |
81 | fn next_u32(&mut self) -> u32 { |
82 | let result_plus: u32 = self.s[0].wrapping_add(self.s[3]); |
83 | impl_xoshiro_u32!(self); |
84 | result_plus |
85 | } |
86 | |
87 | #[inline ] |
88 | fn next_u64(&mut self) -> u64 { |
89 | next_u64_via_u32(self) |
90 | } |
91 | |
92 | #[inline ] |
93 | fn fill_bytes(&mut self, dest: &mut [u8]) { |
94 | fill_bytes_via_next(self, dest); |
95 | } |
96 | } |
97 | |
98 | #[cfg (test)] |
99 | mod tests { |
100 | use super::*; |
101 | |
102 | #[test ] |
103 | fn reference() { |
104 | let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); |
105 | // These values were produced with the reference implementation: |
106 | // http://xoshiro.di.unimi.it/xoshiro128plus.c |
107 | let expected = [ |
108 | 5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097, 4110231701, 399823256, |
109 | 2144435200, |
110 | ]; |
111 | for &e in &expected { |
112 | assert_eq!(rng.next_u32(), e); |
113 | } |
114 | } |
115 | |
116 | #[test ] |
117 | fn test_jump() { |
118 | let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); |
119 | rng.jump(); |
120 | // These values were produced by instrumenting the reference implementation: |
121 | // http://xoshiro.di.unimi.it/xoshiro128plus.c |
122 | assert_eq!(rng.s[0], 2843103750); |
123 | assert_eq!(rng.s[1], 2038079848); |
124 | assert_eq!(rng.s[2], 1533207345); |
125 | assert_eq!(rng.s[3], 44816753); |
126 | } |
127 | |
128 | #[test ] |
129 | fn test_long_jump() { |
130 | let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); |
131 | rng.long_jump(); |
132 | // These values were produced by instrumenting the reference implementation: |
133 | // http://xoshiro.di.unimi.it/xoshiro128plus.c |
134 | assert_eq!(rng.s[0], 1611968294); |
135 | assert_eq!(rng.s[1], 2125834322); |
136 | assert_eq!(rng.s[2], 966769569); |
137 | assert_eq!(rng.s[3], 3193880526); |
138 | } |
139 | } |
140 | |