pub struct SHR3 { /* private fields */ }Expand description
SHR3 – 3-shift-register random number generator
Reading between the lines, I believe the SHR3 defined in Marsaglia’s 1999 post actually has a typo: the shift values defined don’t actually produce a period of 2^32-1, but have 64 possible cycles, some extremely short. But the swapped values from Marsaglia’s 2003 post produce the full 2^32-1 period. So we use that definition of SHR3.
SHR3 is a 3-shift-register generator with period 2^32-1. It uses y[n]=yn-1(I+R^17)(I+L^5), with the y’s viewed as binary vectors, L the 32x32 binary matrix that shifts a vector left 1, and R its transpose.
Marsaglia says: SHR3 seems to pass all except those related to the binary rank test, since 32 successive values, as binary vectors, must be linearly independent, while 32 successive truly random 32-bit integers, viewed as binary vectors, will be linearly independent only about 29% of the time.
use rand_core::RngCore;
use simplerandom::RngJumpAhead;
let mut s = simplerandom::SHR3::new(1);
let r = s.next_u32();
assert_eq!(r, 270369);
let r = s.next_u32();
assert_eq!(r, 67634689);
let r = s.next_u32();
assert_eq!(r, 2647435461);
let r = s.next_u32();
assert_eq!(r, 307599695);
s.jumpahead(1_000_000);
let r = s.next_u32();
assert_eq!(r, 1105614340);Implementations§
Trait Implementations§
Source§impl RngCore for SHR3
impl RngCore for SHR3
Source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest with random data. Read more