Skip to main content

SeededRng

Struct SeededRng 

Source
pub struct SeededRng { /* private fields */ }
Expand description

A seeded xoshiro256++ generator with a standard normal sampler.

It serializes as its 256-bit state, written as four hexadecimal strings ("0x…", because JSON readers such as JavaScript’s lose integers above 2⁵³), and the spare normal deviate, so a run can be checkpointed and resumed bit for bit. The all-zero state is rejected: xoshiro would stay at zero forever.

Implementations§

Source§

impl SeededRng

Source

pub fn seed_from_u64(seed: u64) -> Self

Seeds the generator: the four state words are the first four SplitMix64 outputs starting from seed. SplitMix64 never gives four zero words in a row, so every seed is valid.

Source

pub fn for_stream(seed: u64, keys: &[u64]) -> Self

A generator for one stream of a seeded run, keyed by keys: a Monte Carlo sample’s index and the variable it draws, say. The stream depends only on seed and keys, so a sample draws the same numbers whatever else the run draws, however many samples it has and however they are shared among threads: the counter-based idea of J. K. Salmon, M. A. Moraes, R. O. Dror and D. E. Shaw, “Parallel random numbers: as easy as 1, 2, 3”, Proc. SC11 (2011), https://doi.org/10.1145/2063384.2063405.

The key folds into one word with SplitMix64’s output function mix, a bijection on 64-bit words: h₀ = seed, hᵢ = mix(hᵢ₋₁ ⊕ mix(kᵢ + γ)) with γ SplitMix64’s increment, and the generator is SeededRng::seed_from_u64(hₙ). Two different keys give the same hₙ only by a collision of 64-bit hashes, about one chance in 2⁶⁴ for a pair of streams; with no keys the stream is seed_from_u64(seed)’s.

Source

pub fn next_u64(&mut self) -> u64

The next 64 random bits (the xoshiro256++ output function and state transition).

Source

pub fn uniform(&mut self) -> f64

A uniform deviate on [0, 1): the top 53 bits of SeededRng::next_u64 times 2⁻⁵³, so every value is a multiple of 2⁻⁵³.

Source

pub fn standard_normal(&mut self) -> f64

A standard normal deviate (mean 0, variance 1) by the Marsaglia–Bray polar method.

Each accepted pair gives two deviates; the second is returned by the next call. On average a pair costs 4/π ≈ 1.27 attempts, two uniforms each.

Trait Implementations§

Source§

impl Clone for SeededRng

Source§

fn clone(&self) -> SeededRng

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SeededRng

Source§

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

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

impl<'de> Deserialize<'de> for SeededRng

Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for SeededRng

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Serialize for SeededRng

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SeededRng

Auto Trait Implementations§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.