Struct Lcg64Xsh32

pub struct Lcg64Xsh32 { /* private fields */ }

A PCG random number generator (XSH RR 64/32 (LCG) variant).

Permuted Congruential Generator with 64-bit state, internal Linear Congruential Generator, and 32-bit output via "xorshift high (bits), random rotation" output function.

This is a 64-bit LCG with explicitly chosen stream with the PCG-XSH-RR output function. This combination is the standard pcg32.

Despite the name, this implementation uses 16 bytes (128 bit) space comprising 64 bits of state and 64 bits stream selector. These are both set by SeedableRng, using a 128-bit seed.

Note that two generators with different stream parameter may be closely correlated.

Implementations

impl Lcg64Xsh32

fn advance(&mut self, delta: u64)

Multi-step advance functions (jump-ahead, jump-back)

The method used here is based on Brown, "Random Number Generation with Arbitrary Stride,", Transactions of the American Nuclear Society (Nov. 1994). The algorithm is very similar to fast exponentiation.

Even though delta is an unsigned integer, we can pass a signed integer to go backwards, it just goes "the long way round".

Using this function is equivalent to calling next_32() delta number of times.

fn new(state: u64, stream: u64) -> Self

Construct an instance compatible with PCG seed and stream.

Note that the highest bit of the stream parameter is discarded to simplify upholding internal invariants.

Note that two generators with different stream parameters may be closely correlated.

PCG specifies the following default values for both parameters:

  • state = 0xcafef00dd15ea5e5
  • stream = 0xa02bdbf7bb3c0a7

impl Lcg64Xsh32

fn state(&self) -> u64

Current state of the generator

fn stream(&self) -> u64

Stream parameter of the generator

Note that PCG only stores an increment, which is always odd. Self::from_state discards the highest bit from the stream by shifting it to the left, so this method shifts the increment by one bit to the right.

fn from_state(state: u64, stream: u64) -> Self

Construct an instance using a pre-initialized state

Unlike Self::new, this method does not mutate state. It may therefore be used with Self::state and Self::stream to reconstruct an RNG.

Note that the highest bit of the stream parameter is discarded to simplify upholding internal invariants.

Trait Implementations

impl Clone for Lcg64Xsh32

fn clone(&self) -> Lcg64Xsh32

impl Debug for Lcg64Xsh32

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

impl Eq for Lcg64Xsh32

impl PartialEq for Lcg64Xsh32

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

impl SeedableRng for Lcg64Xsh32

type Seed = [u8; 16];
fn from_seed(seed: Self::Seed) -> Self

We use a single 127-bit seed to initialise the state and select a stream. One seed bit (lowest bit of seed[8]) is ignored.

impl Serialize for Lcg64Xsh32

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where
    __S: Serializer,

impl StructuralPartialEq for Lcg64Xsh32

impl TryRng for Lcg64Xsh32

type Error = never;
fn try_next_u32(&mut self) -> Result<u32, Infallible>
fn try_next_u64(&mut self) -> Result<u64, Infallible>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible>

impl<'de> Deserialize<'de> for Lcg64Xsh32

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where
    __D: Deserializer<'de>,

Auto Trait Implementations

impl Freeze for Lcg64Xsh32

impl RefUnwindSafe for Lcg64Xsh32

impl Send for Lcg64Xsh32

impl Sync for Lcg64Xsh32

impl Unpin for Lcg64Xsh32

impl UnsafeUnpin for Lcg64Xsh32

impl UnwindSafe for Lcg64Xsh32

Blanket Implementations

impl<R> Rng for Lcg64Xsh32 where R: TryRng<Error = never> + ?Sized,

fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dst: &mut [u8])

impl<R> RngCore for Lcg64Xsh32 where R: Rng,

impl<R> TryRngCore for Lcg64Xsh32 where R: TryRng,

type Error = <R as TryRng>::Error;

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> DeserializeOwned for Lcg64Xsh32 where T: for<'de> Deserialize<'de>,

impl<T> From<T> for Lcg64Xsh32

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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