| 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 | /// A xoroshiro128++ random number generator. |
| 16 | /// |
| 17 | /// The xoroshiro128++ algorithm is not suitable for cryptographic purposes, but |
| 18 | /// is very fast and has excellent statistical properties. |
| 19 | /// |
| 20 | /// The algorithm used here is translated from [the `xoroshiro128plusplus.c` |
| 21 | /// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plusplus.c) by |
| 22 | /// David Blackman and Sebastiano Vigna. |
| 23 | #[allow (missing_copy_implementations)] |
| 24 | #[derive (Debug, Clone, PartialEq, Eq)] |
| 25 | #[cfg_attr (feature = "serde" , derive(Serialize, Deserialize))] |
| 26 | pub struct Xoroshiro128PlusPlus { |
| 27 | s0: u64, |
| 28 | s1: u64, |
| 29 | } |
| 30 | |
| 31 | impl Xoroshiro128PlusPlus { |
| 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::Xoroshiro128PlusPlus; |
| 40 | /// |
| 41 | /// let rng1 = Xoroshiro128PlusPlus::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, [0x2bd7a6a6e99c2ddc, 0x0992ccaf6a6fca05]); |
| 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, [0x360fd5f2cf8d5d99, 0x9c6e6877736c46e3]); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl RngCore for Xoroshiro128PlusPlus { |
| 62 | #[inline ] |
| 63 | fn next_u32(&mut self) -> u32 { |
| 64 | self.next_u64() as u32 |
| 65 | } |
| 66 | |
| 67 | #[inline ] |
| 68 | fn next_u64(&mut self) -> u64 { |
| 69 | let r: u64 = plusplus_u64!(self.s0, self.s1, 17); |
| 70 | impl_xoroshiro_u64_plusplus!(self); |
| 71 | r |
| 72 | } |
| 73 | |
| 74 | #[inline ] |
| 75 | fn fill_bytes(&mut self, dest: &mut [u8]) { |
| 76 | fill_bytes_via_next(self, dest); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl SeedableRng for Xoroshiro128PlusPlus { |
| 81 | type Seed = [u8; 16]; |
| 82 | |
| 83 | /// Create a new `Xoroshiro128PlusPlus`. If `seed` is entirely 0, it will be |
| 84 | /// mapped to a different seed. |
| 85 | fn from_seed(seed: [u8; 16]) -> Xoroshiro128PlusPlus { |
| 86 | deal_with_zero_seed!(seed, Self, 16); |
| 87 | let mut s: [u64; 2] = [0; 2]; |
| 88 | read_u64_into(&seed, &mut s); |
| 89 | |
| 90 | Xoroshiro128PlusPlus { s0: s[0], s1: s[1] } |
| 91 | } |
| 92 | |
| 93 | /// Seed a `Xoroshiro128PlusPlus` from a `u64` using `SplitMix64`. |
| 94 | fn seed_from_u64(seed: u64) -> Xoroshiro128PlusPlus { |
| 95 | from_splitmix!(seed) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[cfg (test)] |
| 100 | mod tests { |
| 101 | use super::*; |
| 102 | |
| 103 | #[test ] |
| 104 | fn reference() { |
| 105 | let mut rng = |
| 106 | Xoroshiro128PlusPlus::from_seed([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); |
| 107 | // These values were produced with the reference implementation: |
| 108 | // http://xoshiro.di.unimi.it/xoshiro128plusplus.c |
| 109 | let expected = [ |
| 110 | 393217, |
| 111 | 669327710093319, |
| 112 | 1732421326133921491, |
| 113 | 11394790081659126983, |
| 114 | 9555452776773192676, |
| 115 | 3586421180005889563, |
| 116 | 1691397964866707553, |
| 117 | 10735626796753111697, |
| 118 | 15216282715349408991, |
| 119 | 14247243556711267923, |
| 120 | ]; |
| 121 | for &e in &expected { |
| 122 | assert_eq!(rng.next_u64(), e); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |