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