From acfcc8828278f6e642aea1a9b5fe4b81bac0c1c2 Mon Sep 17 00:00:00 2001 From: Walter de Jong Date: Sun, 3 Jul 2022 23:08:15 +0200 Subject: [PATCH 1/3] fix issue #40 : Xoshiro128Plus is missing long_jump() by adding it --- rand_xoshiro/src/xoshiro128plus.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rand_xoshiro/src/xoshiro128plus.rs b/rand_xoshiro/src/xoshiro128plus.rs index 89cdc30e..b48d50fa 100644 --- a/rand_xoshiro/src/xoshiro128plus.rs +++ b/rand_xoshiro/src/xoshiro128plus.rs @@ -45,6 +45,15 @@ impl Xoshiro128Plus { 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 Xoshiro128Plus { From 24e2d95f1353a8430670ddf1dc844474804e9939 Mon Sep 17 00:00:00 2001 From: Walter de Jong Date: Mon, 4 Jul 2022 19:53:40 +0200 Subject: [PATCH 2/3] add tests for jump() and long_jump() --- rand_xoshiro/src/xoshiro128plus.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/rand_xoshiro/src/xoshiro128plus.rs b/rand_xoshiro/src/xoshiro128plus.rs index b48d50fa..fb2d38e7 100644 --- a/rand_xoshiro/src/xoshiro128plus.rs +++ b/rand_xoshiro/src/xoshiro128plus.rs @@ -118,4 +118,30 @@ mod tests { assert_eq!(rng.next_u32(), e); } } + + #[test] + fn test_jump() { + let mut rng = Xoshiro128Plus::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 2843103750); + assert_eq!(rng.s[1], 2038079848); + assert_eq!(rng.s[2], 1533207345); + assert_eq!(rng.s[3], 44816753); + } + + #[test] + fn test_long_jump() { + let mut rng = Xoshiro128Plus::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.long_jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 1611968294); + assert_eq!(rng.s[1], 2125834322); + assert_eq!(rng.s[2], 966769569); + assert_eq!(rng.s[3], 3193880526); + } } From 59001dfee37852f54dc2c95c7b43d16138907fb4 Mon Sep 17 00:00:00 2001 From: Walter de Jong Date: Mon, 4 Jul 2022 20:04:23 +0200 Subject: [PATCH 3/3] add tests for jump() and long_jump() to Xoshiro128PlusPlus and Xoshiro128StarStar --- rand_xoshiro/src/xoshiro128plusplus.rs | 26 ++++++++++++++++++++++++++ rand_xoshiro/src/xoshiro128starstar.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/rand_xoshiro/src/xoshiro128plusplus.rs b/rand_xoshiro/src/xoshiro128plusplus.rs index 657754ac..3fa11712 100644 --- a/rand_xoshiro/src/xoshiro128plusplus.rs +++ b/rand_xoshiro/src/xoshiro128plusplus.rs @@ -117,4 +117,30 @@ mod tests { assert_eq!(rng.next_u32(), e); } } + + #[test] + fn test_jump() { + let mut rng = Xoshiro128PlusPlus::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 2843103750); + assert_eq!(rng.s[1], 2038079848); + assert_eq!(rng.s[2], 1533207345); + assert_eq!(rng.s[3], 44816753); + } + + #[test] + fn test_long_jump() { + let mut rng = Xoshiro128PlusPlus::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.long_jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 1611968294); + assert_eq!(rng.s[1], 2125834322); + assert_eq!(rng.s[2], 966769569); + assert_eq!(rng.s[3], 3193880526); + } } diff --git a/rand_xoshiro/src/xoshiro128starstar.rs b/rand_xoshiro/src/xoshiro128starstar.rs index 6f416164..449d07c6 100644 --- a/rand_xoshiro/src/xoshiro128starstar.rs +++ b/rand_xoshiro/src/xoshiro128starstar.rs @@ -117,4 +117,30 @@ mod tests { assert_eq!(rng.next_u32(), e); } } + + #[test] + fn test_jump() { + let mut rng = Xoshiro128StarStar::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 2843103750); + assert_eq!(rng.s[1], 2038079848); + assert_eq!(rng.s[2], 1533207345); + assert_eq!(rng.s[3], 44816753); + } + + #[test] + fn test_long_jump() { + let mut rng = Xoshiro128StarStar::from_seed( + [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + rng.long_jump(); + // These values were produced by instrumenting the reference implementation: + // http://xoshiro.di.unimi.it/xoshiro128plus.c + assert_eq!(rng.s[0], 1611968294); + assert_eq!(rng.s[1], 2125834322); + assert_eq!(rng.s[2], 966769569); + assert_eq!(rng.s[3], 3193880526); + } }