SHR3

Struct SHR3 

Source
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§

Source§

impl SHR3

Source

pub fn new(seed1: u32) -> SHR3

Trait Implementations§

Source§

impl Clone for SHR3

Source§

fn clone(&self) -> SHR3

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SHR3

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SHR3

Source§

fn eq(&self, other: &SHR3) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RngCore for SHR3

Source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
Source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
Source§

fn fill_bytes(&mut self, dest: &mut [u8])

Fill dest with random data. Read more
Source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>

Fill dest entirely with random data. Read more
Source§

impl RngJumpAhead for SHR3

Source§

fn jumpahead<N>(&mut self, n: N)
where N: IntTypes,

Source§

impl Eq for SHR3

Source§

impl StructuralPartialEq for SHR3

Auto Trait Implementations§

§

impl Freeze for SHR3

§

impl RefUnwindSafe for SHR3

§

impl Send for SHR3

§

impl Sync for SHR3

§

impl Unpin for SHR3

§

impl UnwindSafe for SHR3

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.