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::le::read_u64_into;
11use rand_core::impls::fill_bytes_via_next;
12use rand_core::{RngCore, SeedableRng};
13
14/// A xoroshiro128+ random number generator.
15///
16/// The xoroshiro128+ 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 `xoroshiro128plus.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plus.c) by
22/// David Blackman and Sebastiano Vigna.
23#[allow(missing_copy_implementations)]
24#[derive(Debug, Clone, PartialEq, Eq)]
25#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
26pub struct Xoroshiro128Plus {
27 s0: u64,
28 s1: u64,
29}
30
31impl Xoroshiro128Plus {
32 /// Jump forward, equivalently to 2^64 calls to `next_u64()`.
33 ///
34 /// This can be used to generate 2^64 non-overlapping subsequences for
35 /// parallel computations.
36 ///
37 /// ```
38 /// use rand_xoshiro::rand_core::SeedableRng;
39 /// use rand_xoshiro::Xoroshiro128Plus;
40 ///
41 /// let rng1 = Xoroshiro128Plus::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!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
49 }
50
51 /// Jump forward, equivalently to 2^96 calls to `next_u64()`.
52 ///
53 /// This can be used to generate 2^32 starting points, from each of which
54 /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
55 /// distributed computations.
56 pub fn long_jump(&mut self) {
57 impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
58 }
59}
60
61impl RngCore for Xoroshiro128Plus {
62 #[inline]
63 fn next_u32(&mut self) -> u32 {
64 // The two lowest bits have some linear dependencies, so we use the
65 // upper bits instead.
66 (self.next_u64() >> 32) as u32
67 }
68
69 #[inline]
70 fn next_u64(&mut self) -> u64 {
71 let r = self.s0.wrapping_add(self.s1);
72 impl_xoroshiro_u64!(self);
73 r
74 }
75
76 #[inline]
77 fn fill_bytes(&mut self, dest: &mut [u8]) {
78 fill_bytes_via_next(self, dest);
79 }
80
81 #[inline]
82 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
83 self.fill_bytes(dest);
84 Ok(())
85 }
86}
87
88impl SeedableRng for Xoroshiro128Plus {
89 type Seed = [u8; 16];
90
91 /// Create a new `Xoroshiro128Plus`. If `seed` is entirely 0, it will be
92 /// mapped to a different seed.
93 fn from_seed(seed: [u8; 16]) -> Xoroshiro128Plus {
94 deal_with_zero_seed!(seed, Self);
95 let mut s: [u64; 2] = [0; 2];
96 read_u64_into(&seed, &mut s);
97
98 Xoroshiro128Plus {
99 s0: s[0],
100 s1: s[1],
101 }
102 }
103
104 /// Seed a `Xoroshiro128Plus` from a `u64` using `SplitMix64`.
105 fn seed_from_u64(seed: u64) -> Xoroshiro128Plus {
106 from_splitmix!(seed)
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn reference() {
116 let mut rng = Xoroshiro128Plus::from_seed(
117 [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
118 // These values were produced with the reference implementation:
119 // http://xoshiro.di.unimi.it/xoshiro128starstar.c
120 let expected = [
121 3, 412333834243, 2360170716294286339, 9295852285959843169,
122 2797080929874688578, 6019711933173041966, 3076529664176959358,
123 3521761819100106140, 7493067640054542992, 920801338098114767,
124 ];
125 for &e in &expected {
126 assert_eq!(rng.next_u64(), e);
127 }
128 }
129}
130