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