Skip to content

Commit 73da25c

Browse files
authored
Merge pull request #41 from walterdejong/40-Xoshiro128Plus-is-missing-long_jump
fix issue #40 : Xoshiro128Plus is missing long_jump()
2 parents 0b59b21 + 59001df commit 73da25c

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

rand_xoshiro/src/xoshiro128plus.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ impl Xoshiro128Plus {
4545
pub fn jump(&mut self) {
4646
impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
4747
}
48+
49+
/// Jump forward, equivalently to 2^96 calls to `next_u32()`.
50+
///
51+
/// This can be used to generate 2^32 starting points, from each of which
52+
/// `jump()` will generate 2^32 non-overlapping subsequences for parallel
53+
/// distributed computations.
54+
pub fn long_jump(&mut self) {
55+
impl_jump!(u32, self, [0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662]);
56+
}
4857
}
4958

5059
impl SeedableRng for Xoshiro128Plus {
@@ -109,4 +118,30 @@ mod tests {
109118
assert_eq!(rng.next_u32(), e);
110119
}
111120
}
121+
122+
#[test]
123+
fn test_jump() {
124+
let mut rng = Xoshiro128Plus::from_seed(
125+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
126+
rng.jump();
127+
// These values were produced by instrumenting the reference implementation:
128+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
129+
assert_eq!(rng.s[0], 2843103750);
130+
assert_eq!(rng.s[1], 2038079848);
131+
assert_eq!(rng.s[2], 1533207345);
132+
assert_eq!(rng.s[3], 44816753);
133+
}
134+
135+
#[test]
136+
fn test_long_jump() {
137+
let mut rng = Xoshiro128Plus::from_seed(
138+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
139+
rng.long_jump();
140+
// These values were produced by instrumenting the reference implementation:
141+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
142+
assert_eq!(rng.s[0], 1611968294);
143+
assert_eq!(rng.s[1], 2125834322);
144+
assert_eq!(rng.s[2], 966769569);
145+
assert_eq!(rng.s[3], 3193880526);
146+
}
112147
}

rand_xoshiro/src/xoshiro128plusplus.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,30 @@ mod tests {
117117
assert_eq!(rng.next_u32(), e);
118118
}
119119
}
120+
121+
#[test]
122+
fn test_jump() {
123+
let mut rng = Xoshiro128PlusPlus::from_seed(
124+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
125+
rng.jump();
126+
// These values were produced by instrumenting the reference implementation:
127+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
128+
assert_eq!(rng.s[0], 2843103750);
129+
assert_eq!(rng.s[1], 2038079848);
130+
assert_eq!(rng.s[2], 1533207345);
131+
assert_eq!(rng.s[3], 44816753);
132+
}
133+
134+
#[test]
135+
fn test_long_jump() {
136+
let mut rng = Xoshiro128PlusPlus::from_seed(
137+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
138+
rng.long_jump();
139+
// These values were produced by instrumenting the reference implementation:
140+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
141+
assert_eq!(rng.s[0], 1611968294);
142+
assert_eq!(rng.s[1], 2125834322);
143+
assert_eq!(rng.s[2], 966769569);
144+
assert_eq!(rng.s[3], 3193880526);
145+
}
120146
}

rand_xoshiro/src/xoshiro128starstar.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,30 @@ mod tests {
117117
assert_eq!(rng.next_u32(), e);
118118
}
119119
}
120+
121+
#[test]
122+
fn test_jump() {
123+
let mut rng = Xoshiro128StarStar::from_seed(
124+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
125+
rng.jump();
126+
// These values were produced by instrumenting the reference implementation:
127+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
128+
assert_eq!(rng.s[0], 2843103750);
129+
assert_eq!(rng.s[1], 2038079848);
130+
assert_eq!(rng.s[2], 1533207345);
131+
assert_eq!(rng.s[3], 44816753);
132+
}
133+
134+
#[test]
135+
fn test_long_jump() {
136+
let mut rng = Xoshiro128StarStar::from_seed(
137+
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
138+
rng.long_jump();
139+
// These values were produced by instrumenting the reference implementation:
140+
// http://xoshiro.di.unimi.it/xoshiro128plus.c
141+
assert_eq!(rng.s[0], 1611968294);
142+
assert_eq!(rng.s[1], 2125834322);
143+
assert_eq!(rng.s[2], 966769569);
144+
assert_eq!(rng.s[3], 3193880526);
145+
}
120146
}

0 commit comments

Comments
 (0)