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; |
10 | use rand_core::le::read_u64_into; |
11 | use rand_core::{RngCore, SeedableRng}; |
12 | #[cfg (feature = "serde" )] |
13 | use serde::{Deserialize, Serialize}; |
14 | |
15 | use crate::Seed512; |
16 | |
17 | /// A xoshiro512** random number generator. |
18 | /// |
19 | /// The xoshiro512** algorithm is not suitable for cryptographic purposes, but |
20 | /// is very fast and has excellent statistical properties. |
21 | /// |
22 | /// The algorithm used here is translated from [the `xoshiro512starstar.c` |
23 | /// reference source code](http://xoshiro.di.unimi.it/xoshiro512starstar.c) by |
24 | /// David Blackman and Sebastiano Vigna. |
25 | #[derive (Debug, Clone, PartialEq, Eq)] |
26 | #[cfg_attr (feature = "serde" , derive(Serialize, Deserialize))] |
27 | pub struct Xoshiro512StarStar { |
28 | s: [u64; 8], |
29 | } |
30 | |
31 | impl Xoshiro512StarStar { |
32 | /// Jump forward, equivalently to 2^256 calls to `next_u64()`. |
33 | /// |
34 | /// This can be used to generate 2^256 non-overlapping subsequences for |
35 | /// parallel computations. |
36 | /// |
37 | /// ``` |
38 | /// use rand_xoshiro::rand_core::SeedableRng; |
39 | /// use rand_xoshiro::Xoshiro512StarStar; |
40 | /// |
41 | /// let rng1 = Xoshiro512StarStar::seed_from_u64(0); |
42 | /// let mut rng2 = rng1.clone(); |
43 | /// rng2.jump(); |
44 | /// let mut rng3 = rng2.clone(); |
45 | /// rng3.jump(); |
46 | /// ``` |
47 | pub fn jump(&mut self) { |
48 | impl_jump!( |
49 | u64, |
50 | self, |
51 | [ |
52 | 0x33ed89b6e7a353f9, |
53 | 0x760083d7955323be, |
54 | 0x2837f2fbb5f22fae, |
55 | 0x4b8c5674d309511c, |
56 | 0xb11ac47a7ba28c25, |
57 | 0xf1be7667092bcc1c, |
58 | 0x53851efdb6df0aaf, |
59 | 0x1ebbc8b23eaf25db |
60 | ] |
61 | ); |
62 | } |
63 | |
64 | /// Jump forward, equivalently to 2^384 calls to `next_u64()`. |
65 | /// |
66 | /// This can be used to generate 2^128 starting points, from each of which |
67 | /// `jump()` will generate 2^128 non-overlapping subsequences for parallel |
68 | /// distributed computations. |
69 | pub fn long_jump(&mut self) { |
70 | impl_jump!( |
71 | u64, |
72 | self, |
73 | [ |
74 | 0x11467fef8f921d28, |
75 | 0xa2a819f2e79c8ea8, |
76 | 0xa8299fc284b3959a, |
77 | 0xb4d347340ca63ee1, |
78 | 0x1cb0940bedbff6ce, |
79 | 0xd956c5c4fa1f8e17, |
80 | 0x915e38fd4eda93bc, |
81 | 0x5b3ccdfa5d7daca5 |
82 | ] |
83 | ); |
84 | } |
85 | } |
86 | |
87 | impl SeedableRng for Xoshiro512StarStar { |
88 | type Seed = Seed512; |
89 | |
90 | /// Create a new `Xoshiro512StarStar`. If `seed` is entirely 0, it will be |
91 | /// mapped to a different seed. |
92 | #[inline ] |
93 | fn from_seed(seed: Seed512) -> Xoshiro512StarStar { |
94 | deal_with_zero_seed!(seed, Self); |
95 | let mut state: [u64; 8] = [0; 8]; |
96 | read_u64_into(&seed.0, &mut state); |
97 | Xoshiro512StarStar { s: state } |
98 | } |
99 | |
100 | /// Seed a `Xoshiro512StarStar` from a `u64` using `SplitMix64`. |
101 | fn seed_from_u64(seed: u64) -> Xoshiro512StarStar { |
102 | from_splitmix!(seed) |
103 | } |
104 | } |
105 | |
106 | impl RngCore for Xoshiro512StarStar { |
107 | #[inline ] |
108 | fn next_u32(&mut self) -> u32 { |
109 | // The lowest bits have some linear dependencies, so we use the |
110 | // upper bits instead. |
111 | (self.next_u64() >> 32) as u32 |
112 | } |
113 | |
114 | #[inline ] |
115 | fn next_u64(&mut self) -> u64 { |
116 | let result_starstar: u64 = starstar_u64!(self.s[1]); |
117 | impl_xoshiro_large!(self); |
118 | result_starstar |
119 | } |
120 | |
121 | #[inline ] |
122 | fn fill_bytes(&mut self, dest: &mut [u8]) { |
123 | fill_bytes_via_next(self, dest); |
124 | } |
125 | } |
126 | |
127 | #[cfg (test)] |
128 | mod tests { |
129 | use super::*; |
130 | |
131 | #[test ] |
132 | #[rustfmt::skip] |
133 | fn reference() { |
134 | let mut rng = Xoshiro512StarStar::from_seed(Seed512( |
135 | [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, |
136 | 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, |
137 | 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, |
138 | 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0])); |
139 | // These values were produced with the reference implementation: |
140 | // http://xoshiro.di.unimi.it/xoshiro512starstar.c |
141 | let expected = [ |
142 | 11520, |
143 | 0, |
144 | 23040, |
145 | 23667840, |
146 | 144955163520, |
147 | 303992986974289920, |
148 | 25332796375735680, |
149 | 296904390158016, |
150 | 13911081092387501979, |
151 | 15304787717237593024, |
152 | ]; |
153 | for &e in &expected { |
154 | assert_eq!(rng.next_u64(), e); |
155 | } |
156 | } |
157 | } |
158 | |