Struct Lcg128CmDxsm64

pub struct Lcg128CmDxsm64 { /* private fields */ }

A PCG random number generator (CM DXSM 128/64 (LCG) variant).

Permuted Congruential Generator with 128-bit state, internal Linear Congruential Generator, and 64-bit output via "double xorshift multiply" output function.

This is a 128-bit LCG with explicitly chosen stream with the PCG-DXSM output function. This corresponds to pcg_engines::cm_setseq_dxsm_128_64 from pcg_cpp and PCG64DXSM from NumPy.

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

Note that while two generators with different stream parameter may be closely correlated, this is mitigated by the DXSM output function.

Implementations

impl Lcg128CmDxsm64

fn state(&self) -> u128

Current state of the generator

fn stream(&self) -> u128

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: u128, stream: u128) -> 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.

impl Lcg128CmDxsm64

fn advance(&mut self, delta: u128)

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_64() delta number of times.

fn new(state: u128, stream: u128) -> 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 while two generators with different stream parameter may be closely correlated, this is mitigated by the DXSM output function.

PCG specifies the following default values for both parameters:

  • state = 0xcafef00dd15ea5e5
  • stream = 0xa02bdbf7bb3c0a7ac28fa16a64abf96

Trait Implementations

impl Clone for Lcg128CmDxsm64

fn clone(&self) -> Lcg128CmDxsm64

impl Debug for Lcg128CmDxsm64

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

impl Eq for Lcg128CmDxsm64

impl PartialEq for Lcg128CmDxsm64

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

impl SeedableRng for Lcg128CmDxsm64

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

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

impl Serialize for Lcg128CmDxsm64

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

impl StructuralPartialEq for Lcg128CmDxsm64

impl TryRng for Lcg128CmDxsm64

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 Lcg128CmDxsm64

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

Auto Trait Implementations

impl Freeze for Lcg128CmDxsm64

impl RefUnwindSafe for Lcg128CmDxsm64

impl Send for Lcg128CmDxsm64

impl Sync for Lcg128CmDxsm64

impl Unpin for Lcg128CmDxsm64

impl UnsafeUnpin for Lcg128CmDxsm64

impl UnwindSafe for Lcg128CmDxsm64

Blanket Implementations

impl<R> Rng for Lcg128CmDxsm64 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 Lcg128CmDxsm64 where R: Rng,

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

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

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

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

impl<T> From<T> for Lcg128CmDxsm64

fn from(t: T) -> T

Returns the argument unchanged.

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

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

impl<T, U> Into<U> for Lcg128CmDxsm64 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 Lcg128CmDxsm64 where U: Into<T>,

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

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

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