Skip to content

Commit 17fbb32

Browse files
committed
For various JumpAhead implementations, sanitise state first
1 parent 9456905 commit 17fbb32

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl RngJumpAhead for SHR3 {
111111
0x20231000, 0x40462021, 0x808C4042, 0x01080084, 0x02100108, 0x04200210, 0x08400420, 0x10800840,
112112
0x21001080, 0x42002100, 0x84004200, 0x08008400, 0x10010800, 0x20021000, 0x40042000, 0x80084000,
113113
];
114+
self.sanitise();
114115
let shr3_matrix = bitcolumnmatrix::BitColumnMatrix32::new(&SHR3_MATRIX_ARRAY);
115116
let shr3_mult = shr3_matrix.pow(n);
116117
self.shr3 = shr3_mult.dot_vec(self.shr3);
@@ -163,15 +164,17 @@ impl MWC2 {
163164
lower: seed2,
164165
}
165166
}
167+
fn sanitise(&mut self) {
168+
self.upper = mwc_sanitise(self.upper, MWC2::UPPER_MOD);
169+
self.lower = mwc_sanitise(self.lower, MWC2::LOWER_MOD);
170+
}
166171
fn current(&self) -> u32 {
167172
self.lower.wrapping_add(self.upper << 16).wrapping_add(self.upper >> 16)
168173
}
169174
}
170175
impl RngCore for MWC2 {
171176
fn next_u32(&mut self) -> u32 {
172-
self.upper = mwc_sanitise(self.upper, MWC2::UPPER_MOD);
173-
self.lower = mwc_sanitise(self.lower, MWC2::LOWER_MOD);
174-
177+
self.sanitise();
175178
self.upper = mwc_next(self.upper, MWC2::UPPER_M);
176179
self.lower = mwc_next(self.lower, MWC2::LOWER_M);
177180

@@ -191,6 +194,7 @@ impl RngJumpAhead for MWC2 {
191194
fn jumpahead<N>(&mut self, n: N)
192195
where N: Unsigned + PrimInt
193196
{
197+
self.sanitise();
194198
self.upper = maths::mul_mod(maths::pow_mod(MWC2::UPPER_M, n, MWC2::UPPER_MOD), self.upper, MWC2::UPPER_MOD);
195199
self.lower = maths::mul_mod(maths::pow_mod(MWC2::LOWER_M, n, MWC2::LOWER_MOD), self.lower, MWC2::LOWER_MOD);
196200
}
@@ -336,6 +340,7 @@ impl RngJumpAhead for MWC64 {
336340
fn jumpahead<N>(&mut self, n: N)
337341
where N: Unsigned + PrimInt
338342
{
343+
self.sanitise();
339344
self.mwc = maths::mul_mod(maths::pow_mod(MWC64::M, n, MWC64::MOD), self.mwc, MWC64::MOD);
340345
}
341346
}

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ fn test_new_and_next_u32() {
99
//let mut s = simplerandom::SHR3::new(1);
1010
//let mut s = simplerandom::MWC1::new(1, 2);
1111
//let mut s = simplerandom::MWC2::new(1, 2);
12-
//let mut s = simplerandom::KISS::new(1, 2, 3, 4);
12+
let mut s = simplerandom::KISS::new(1, 2, 3, 4);
1313
//let mut s = simplerandom::MWC64::new(1, 2);
1414
//let mut s = simplerandom::KISS2::new(1, 2, 3, 4);
1515
//let mut s = simplerandom::LFSR88::new(1, 2, 3);
16-
let mut s = simplerandom::LFSR113::new(1, 2, 3, 4);
16+
//let mut s = simplerandom::LFSR113::new(1, 2, 3, 4);
1717
for _ in 0..4 {
1818
println!("{}, {:?}", s.next_u32(), s);
1919
//println!("{}, {:?}", s.gen::<u32>(), s);
2020
//println!("{:?}", s.gen::<(f64)>());
2121
}
2222

23-
let jumpahead_n = 1_000_000_000_u32;
23+
let jumpahead_n = 1_000_000_000_000_000_000_u64;
2424
s.jumpahead(jumpahead_n);
2525
println!("jumpahead by {}", jumpahead_n);
2626
println!("{}, {:?}", s.next_u32(), s);

0 commit comments

Comments
 (0)