Merge pull request #879 from vks/update-average
Update dependency on `average`
diff --git a/rand_xoshiro/src/common.rs b/rand_xoshiro/src/common.rs
index b188dd6..1af3543 100644
--- a/rand_xoshiro/src/common.rs
+++ b/rand_xoshiro/src/common.rs
@@ -28,6 +28,20 @@
}
}
+/// Apply the ++ scrambler used by some RNGs from the xoshiro family.
+macro_rules! plusplus_u64 {
+ ($x:expr, $y:expr, $rot:expr) => {
+ $x.wrapping_add($y).rotate_left($rot).wrapping_add($x)
+ }
+}
+
+/// Apply the ++ scrambler used by some RNGs from the xoshiro family.
+macro_rules! plusplus_u32 {
+ ($x:expr, $y:expr) => {
+ $x.wrapping_add($y).rotate_left(7).wrapping_add($x)
+ }
+}
+
/// Implement a jump function for an RNG from the xoshiro family.
macro_rules! impl_jump {
(u32, $self:expr, [$j0:expr, $j1:expr]) => {
@@ -147,6 +161,15 @@
}
}
+/// Implement the xoroshiro iteration for the ++ scrambler.
+macro_rules! impl_xoroshiro_u64_plusplus {
+ ($self:expr) => {
+ $self.s1 ^= $self.s0;
+ $self.s0 = $self.s0.rotate_left(49) ^ $self.s1 ^ ($self.s1 << 21);
+ $self.s1 = $self.s1.rotate_left(28);
+ }
+}
+
/// Implement the xoshiro iteration for `u32` output.
macro_rules! impl_xoshiro_u32 {
($self:expr) => {
diff --git a/rand_xoshiro/src/lib.rs b/rand_xoshiro/src/lib.rs
index 3047e92..14a691c 100644
--- a/rand_xoshiro/src/lib.rs
+++ b/rand_xoshiro/src/lib.rs
@@ -17,6 +17,8 @@
//! # 64-bit generators
//! - [`Xoshiro256StarStar`]: Recommended for all purposes. Excellent speed and
//! a state space (256 bits) large enough for any parallel application.
+//! - [`Xoshiro256PlusPlus`]: Recommended for all purposes. Excellent speed and
+//! a state space (256 bits) large enough for any parallel application.
//! - [`Xoshiro256Plus`]: Recommended for generating 64-bit floating-point
//! numbers. About 15% faster than `Xoshiro256StarStar`, but has a [low linear
//! complexity] in the lowest bits (which are discarded when generating
@@ -25,6 +27,9 @@
//! - [`Xoroshiro128StarStar`]: An alternative to `Xoshiro256StarStar`, having
//! the same speed but using half the state. Only suited for low-scale parallel
//! applications.
+//! - [`Xoroshiro128PlusPlus`]: An alternative to `Xoshiro256PlusPlus`, having
+//! the same speed but using half the state. Only suited for low-scale parallel
+//! applications.
//! - [`Xoroshiro128Plus`]: An alternative to `Xoshiro256Plus`, having the same
//! speed but using half the state. Only suited for low-scale parallel
//! applications. Has a [low linear complexity] in the lowest bits (which are
@@ -32,6 +37,8 @@
//! unlikely to have any impact in practise.
//! - [`Xoshiro512StarStar`]: An alternative to `Xoshiro256StarStar` with more
//! state and the same speed.
+//! - [`Xoshiro512PlusPlus`]: An alternative to `Xoshiro256PlusPlus` with more
+//! state and the same speed.
//! - [`Xoshiro512Plus`]: An alternative to `Xoshiro512Plus` with more
//! state and the same speed. Has a [low linear complexity] in the lowest bits
//! (which are discarded when generating floats), making it fail linearity
@@ -41,6 +48,7 @@
//!
//! # 32-bit generators
//! - [`Xoshiro128StarStar`]: Recommended for all purposes. Excellent speed.
+//! - [`Xoshiro128PlusPlus`]: Recommended for all purposes. Excellent speed.
//! - [`Xoshiro128Plus`]: Recommended for generating 32-bit floating-point
//! numbers. Faster than `Xoshiro128StarStar`, but has a [low linear
//! complexity] in the lowest bits (which are discarded when generating
@@ -53,7 +61,11 @@
//! lowest bits (which are discarded when generating floats), making it fail
//! linearity tests. This is unlikely to have any impact in practise.
//!
+//! The `*PlusPlus` generators perform similarily to the `*StarStar` generators.
+//! See the [xoshiro paper], where the differences are discussed in detail.
+//!
//! [xoshiro]: http://xoshiro.di.unimi.it/
+//! [xoshiro paper]: http://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf
//! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
@@ -69,12 +81,16 @@
mod common;
mod splitmix64;
mod xoshiro128starstar;
+mod xoshiro128plusplus;
mod xoshiro128plus;
mod xoshiro256starstar;
+mod xoshiro256plusplus;
mod xoshiro256plus;
mod xoshiro512starstar;
+mod xoshiro512plusplus;
mod xoshiro512plus;
mod xoroshiro128plus;
+mod xoroshiro128plusplus;
mod xoroshiro128starstar;
mod xoroshiro64starstar;
mod xoroshiro64star;
@@ -82,13 +98,17 @@
pub use rand_core;
pub use splitmix64::SplitMix64;
pub use xoshiro128starstar::Xoshiro128StarStar;
+pub use xoshiro128plusplus::Xoshiro128PlusPlus;
pub use xoshiro128plus::Xoshiro128Plus;
pub use xoshiro256starstar::Xoshiro256StarStar;
+pub use xoshiro256plusplus::Xoshiro256PlusPlus;
pub use xoshiro256plus::Xoshiro256Plus;
pub use common::Seed512;
pub use xoshiro512starstar::Xoshiro512StarStar;
+pub use xoshiro512plusplus::Xoshiro512PlusPlus;
pub use xoshiro512plus::Xoshiro512Plus;
pub use xoroshiro128plus::Xoroshiro128Plus;
pub use xoroshiro128starstar::Xoroshiro128StarStar;
+pub use xoroshiro128plusplus::Xoroshiro128PlusPlus;
pub use xoroshiro64starstar::Xoroshiro64StarStar;
pub use xoroshiro64star::Xoroshiro64Star;
diff --git a/rand_xoshiro/src/xoroshiro128plusplus.rs b/rand_xoshiro/src/xoroshiro128plusplus.rs
new file mode 100644
index 0000000..fa6bc68
--- /dev/null
+++ b/rand_xoshiro/src/xoroshiro128plusplus.rs
@@ -0,0 +1,127 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
+use rand_core;
+use rand_core::le::read_u64_into;
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::{RngCore, SeedableRng};
+
+/// A xoroshiro128++ random number generator.
+///
+/// The xoroshiro128++ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoroshiro128plusplus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plusplus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
+pub struct Xoroshiro128PlusPlus {
+ s0: u64,
+ s1: u64,
+}
+
+impl Xoroshiro128PlusPlus {
+ /// Jump forward, equivalently to 2^64 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// use rand_xoshiro::rand_core::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128PlusPlus;
+ ///
+ /// let rng1 = Xoroshiro128PlusPlus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [0x2bd7a6a6e99c2ddc, 0x0992ccaf6a6fca05]);
+ }
+
+ /// Jump forward, equivalently to 2^96 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^32 starting points, from each of which
+ /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [0x360fd5f2cf8d5d99, 0x9c6e6877736c46e3]);
+ }
+}
+
+impl RngCore for Xoroshiro128PlusPlus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let r = plusplus_u64!(self.s0, self.s1, 17);
+ impl_xoroshiro_u64_plusplus!(self);
+ r
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for Xoroshiro128PlusPlus {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoroshiro128PlusPlus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ fn from_seed(seed: [u8; 16]) -> Xoroshiro128PlusPlus {
+ deal_with_zero_seed!(seed, Self);
+ let mut s = [0; 2];
+ read_u64_into(&seed, &mut s);
+
+ Xoroshiro128PlusPlus {
+ s0: s[0],
+ s1: s[1],
+ }
+ }
+
+ /// Seed a `Xoroshiro128PlusPlus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoroshiro128PlusPlus {
+ from_splitmix!(seed)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoroshiro128PlusPlus::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128plusplus.c
+ let expected = [
+ 393217, 669327710093319, 1732421326133921491, 11394790081659126983,
+ 9555452776773192676, 3586421180005889563, 1691397964866707553,
+ 10735626796753111697, 15216282715349408991, 14247243556711267923,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand_xoshiro/src/xoshiro128plusplus.rs b/rand_xoshiro/src/xoshiro128plusplus.rs
new file mode 100644
index 0000000..3596c77
--- /dev/null
+++ b/rand_xoshiro/src/xoshiro128plusplus.rs
@@ -0,0 +1,120 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
+use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
+use rand_core::le::read_u32_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro128++ random number generator.
+///
+/// The xoshiro128++ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro128plusplus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro128plusplus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
+pub struct Xoshiro128PlusPlus {
+ s: [u32; 4],
+}
+
+impl Xoshiro128PlusPlus {
+ /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// use rand_xoshiro::rand_core::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128PlusPlus;
+ ///
+ /// let rng1 = Xoroshiro128PlusPlus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
+ }
+
+ /// Jump forward, equivalently to 2^96 calls to `next_u32()`.
+ ///
+ /// This can be used to generate 2^32 starting points, from each of which
+ /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]);
+ }
+}
+
+impl SeedableRng for Xoshiro128PlusPlus {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoshiro128PlusPlus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u32_into(&seed, &mut state);
+ Xoshiro128PlusPlus { s: state }
+ }
+
+ /// Seed a `Xoshiro128PlusPlus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro128PlusPlus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro128PlusPlus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ let result_starstar = plusplus_u32!(self.s[0], self.s[3]);
+ impl_xoshiro_u32!(self);
+ result_starstar
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ next_u64_via_u32(self)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro128PlusPlus::from_seed(
+ [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128plusplus.c
+ let expected = [
+ 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768,
+ 3867114732, 1355841295, 495546011, 621204420,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand_xoshiro/src/xoshiro128starstar.rs b/rand_xoshiro/src/xoshiro128starstar.rs
index 7af1e50..a355bbc 100644
--- a/rand_xoshiro/src/xoshiro128starstar.rs
+++ b/rand_xoshiro/src/xoshiro128starstar.rs
@@ -44,6 +44,15 @@
pub fn jump(&mut self) {
impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
}
+
+ /// Jump forward, equivalently to 2^96 calls to `next_u32()`.
+ ///
+ /// This can be used to generate 2^32 starting points, from each of which
+ /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]);
+ }
}
impl SeedableRng for Xoshiro128StarStar {
@@ -68,7 +77,7 @@
impl RngCore for Xoshiro128StarStar {
#[inline]
fn next_u32(&mut self) -> u32 {
- let result_starstar = starstar_u64!(self.s[0]);
+ let result_starstar = starstar_u64!(self.s[1]);
impl_xoshiro_u32!(self);
result_starstar
}
@@ -98,11 +107,11 @@
fn reference() {
let mut rng = Xoshiro128StarStar::from_seed(
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
- // These values were produced with the reference implementation:
+ // These values were produced with the reference implementation (v1.1):
// http://xoshiro.di.unimi.it/xoshiro128starstar.c
let expected = [
- 5760, 40320, 70819200, 3297914139, 2480851620, 1792823698,
- 4118739149, 1251203317, 1581886583, 1721184582,
+ 11520, 0, 5927040, 70819200, 2031721883, 1637235492, 1287239034,
+ 3734860849, 3729100597, 4258142804,
];
for &e in &expected {
assert_eq!(rng.next_u32(), e);
diff --git a/rand_xoshiro/src/xoshiro256plusplus.rs b/rand_xoshiro/src/xoshiro256plusplus.rs
new file mode 100644
index 0000000..649e081
--- /dev/null
+++ b/rand_xoshiro/src/xoshiro256plusplus.rs
@@ -0,0 +1,128 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro256** random number generator.
+///
+/// The xoshiro256** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro256plusplus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plusplus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
+pub struct Xoshiro256PlusPlus {
+ s: [u64; 4],
+}
+
+impl Xoshiro256PlusPlus {
+ /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// use rand_xoshiro::rand_core::SeedableRng;
+ /// use rand_xoshiro::Xoshiro256PlusPlus;
+ ///
+ /// let rng1 = Xoshiro256PlusPlus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
+ 0xa9582618e03fc9aa, 0x39abdc4529b1661c
+ ]);
+ }
+
+ /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 starting points, from each of which
+ /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
+ 0x77710069854ee241, 0x39109bb02acbe635
+ ]);
+ }
+}
+
+impl SeedableRng for Xoshiro256PlusPlus {
+ type Seed = [u8; 32];
+
+ /// Create a new `Xoshiro256PlusPlus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u64_into(&seed, &mut state);
+ Xoshiro256PlusPlus { s: state }
+ }
+
+ /// Seed a `Xoshiro256PlusPlus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro256PlusPlus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro256PlusPlus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_plusplus = plusplus_u64!(self.s[0], self.s[3], 23);
+ impl_xoshiro_u64!(self);
+ result_plusplus
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro256PlusPlus::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro256plusplus.c
+ let expected = [
+ 41943041, 58720359, 3588806011781223, 3591011842654386,
+ 9228616714210784205, 9973669472204895162, 14011001112246962877,
+ 12406186145184390807, 15849039046786891736, 10450023813501588000,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand_xoshiro/src/xoshiro512plus.rs b/rand_xoshiro/src/xoshiro512plus.rs
index 4b589f2..baa3480 100644
--- a/rand_xoshiro/src/xoshiro512plus.rs
+++ b/rand_xoshiro/src/xoshiro512plus.rs
@@ -51,6 +51,19 @@
0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
]);
}
+
+ /// Jump forward, equivalently to 2^384 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 starting points, from each of which
+ /// `jump()` will generate 2^128 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a,
+ 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17,
+ 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5
+ ]);
+ }
}
impl SeedableRng for Xoshiro512Plus {
diff --git a/rand_xoshiro/src/xoshiro512plusplus.rs b/rand_xoshiro/src/xoshiro512plusplus.rs
new file mode 100644
index 0000000..6f967f5
--- /dev/null
+++ b/rand_xoshiro/src/xoshiro512plusplus.rs
@@ -0,0 +1,135 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+use crate::Seed512;
+
+/// A xoshiro512++ random number generator.
+///
+/// The xoshiro512++ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro512plusplus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro512plusplus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
+pub struct Xoshiro512PlusPlus {
+ s: [u64; 8],
+}
+
+impl Xoshiro512PlusPlus {
+ /// Jump forward, equivalently to 2^256 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^256 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// use rand_xoshiro::rand_core::SeedableRng;
+ /// use rand_xoshiro::Xoshiro512PlusPlus;
+ ///
+ /// let rng1 = Xoshiro512PlusPlus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae,
+ 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c,
+ 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
+ ]);
+ }
+
+ /// Jump forward, equivalently to 2^384 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 starting points, from each of which
+ /// `jump()` will generate 2^128 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a,
+ 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17,
+ 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5
+ ]);
+ }
+}
+
+
+impl SeedableRng for Xoshiro512PlusPlus {
+ type Seed = Seed512;
+
+ /// Create a new `Xoshiro512PlusPlus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: Seed512) -> Xoshiro512PlusPlus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 8];
+ read_u64_into(&seed.0, &mut state);
+ Xoshiro512PlusPlus { s: state }
+ }
+
+ /// Seed a `Xoshiro512PlusPlus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro512PlusPlus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro512PlusPlus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_plusplus = plusplus_u64!(self.s[2], self.s[0], 17);
+ impl_xoshiro_large!(self);
+ result_plusplus
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro512PlusPlus::from_seed(Seed512(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
+ 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,
+ 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]));
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro512plusplus.c
+ let expected = [
+ 524291, 1048578, 539099140, 3299073855497, 6917532603230064654,
+ 7494048333530275843, 14418333309547923463, 10960079161595355914,
+ 18279570946505382726, 10209173166699159237,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand_xoshiro/src/xoshiro512starstar.rs b/rand_xoshiro/src/xoshiro512starstar.rs
index 2db9ac1..8483944 100644
--- a/rand_xoshiro/src/xoshiro512starstar.rs
+++ b/rand_xoshiro/src/xoshiro512starstar.rs
@@ -50,6 +50,19 @@
0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
]);
}
+
+ /// Jump forward, equivalently to 2^384 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 starting points, from each of which
+ /// `jump()` will generate 2^128 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a,
+ 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17,
+ 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5
+ ]);
+ }
}