moonrand 0.2.1 · Apache-2.0

reference

Every public itemthe compiler's own list

This page is generated from the interfaces moon info writes and the /// prose above each declaration: 592 items over 23 packages, of which 63 are generator types. The signature is the compiler's; the paragraph under it is the source's.

01 / the root

moonbitstack/moonrand36 items

The default generator, the sampling functions and the distributions.

functions

pub fn[R : @contract.Rng] bernoulli(r : R, p~ : Double = 0.5) -> Bool

true with probability p, and false the rest of the time, so that a thousand calls hand back about 1000p of them. p outside [0, 1] is answered by the nearer end: certainty or impossibility.

pub fn[R : @contract.Rng] betavariate(r : R, alpha : Double, beta : Double) -> Double

A beta draw with shape parameters alpha and beta: two gamma draws, taken as a fraction of their sum.

pub fn[R : @contract.Rng] binomialvariate(r : R, n : Int64, p : Double) -> Int64

The number of successes in n trials with success probability p, by inverting the binomial CDF term by term: exact for any n, and linear in the number of successes drawn.

pub fn[R : @contract.Rng] cauchy(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double

A Cauchy draw: the location loc and the scale scale. Its mean and variance do not exist, so the median is the only centre it has — and a sample of it will surprise anyone who expects one.

pub fn[R : @contract.Rng] chi_squared(r : R, df : Double) -> Double

A chi-squared draw with k degrees of freedom: a gamma draw of shape k/2 and scale two, which is what the chi-squared distribution is.

pub fn[R : @contract.Rng, T] choice(r : R, items : ArrayView[T]) -> T?

One element, or None for an empty view.

pub fn[R : @contract.Rng, T] choices(r : R, items : ArrayView[T], k~ : Int32 = 1, weights? : ArrayView[Double]) -> Array[T]

k elements with replacement; with weights they are drawn proportionally.

pub fn[R : @contract.Rng] dirichlet(r : R, alpha : ArrayView[Double]) -> Array[Double]

A Dirichlet draw of concentration alpha: one gamma draw per component, each scaled down by their sum, so the result adds up to one. Every component of alpha must be positive.

pub fn[R : @contract.Rng] expovariate(r : R, lambd : Double) -> Double

An exponential draw with rate lambd (mean 1/lambd).

pub fn[R : @contract.Rng] fisher_f(r : R, d1 : Double, d2 : Double) -> Double

An F draw with d1 and d2 degrees of freedom: the ratio of two chi-squared draws, each divided by its own degrees of freedom.

pub fn[R : @contract.Rng] gammavariate(r : R, alpha : Double, beta : Double) -> Double

A gamma draw with shape alpha and scale beta, by Marsaglia and Tsang's transform-reject method with the squeeze from their paper (and the Cheng variant for alpha below one).

pub fn[R : @contract.Rng] gauss(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double

The distributions, one draw each, over any Rng. Names follow Python's random where they overlap; several more are built from those by the definition of the distribution.

A draw takes as many words from the stream as its algorithm needs, so two distributions asked of the same generator interleave in the order you ask. Nothing is cached between calls: a normal draw is one draw, not one of a stored pair.

A normal draw, scaled to mu and sigma (Box-Muller).

pub fn[R : @contract.Rng] geometric(r : R, p : Double) -> Int64

The number of trials up to and including the first success, with success probability p: the support is 1, 2, 3, and the mean is 1/p.

pub fn[R : @contract.Rng] getrandbits(r : R, k : Int32) -> UInt64

k low bits of the next word; k over 64 returns the whole word.

pub fn[R : @contract.Rng] gumbel(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double

A Gumbel draw, the distribution of a maximum: location loc, scale scale, mean loc + 0.5772 scale.

pub fn[R : @contract.Rng] laplace(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double

A Laplace draw: the two-sided exponential, centred on loc, as wide as scale, with variance 2 scale^2.

pub fn[R : @contract.Rng] logistic(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double

A logistic draw with location loc and scale scale: the log-odds of one uniform, which is what the distribution is.

pub fn[R : @contract.Rng] lognormvariate(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double

A lognormal draw: exp(gauss(mu, sigma)).

pub fn[R : @contract.Rng] normalvariate(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double

A normal draw by the ratio-of-uniforms method of Kinderman and Monahan, the one Python's normalvariate uses — a different algorithm from gauss, and a different stream for the same seed.

pub fn[R : @contract.Rng] paretovariate(r : R, alpha : Double) -> Double

A Pareto draw with shape alpha: one minus a uniform, raised to -1/alpha.

pub fn[R : @contract.Rng] permutation(r : R, n : Int32) -> Array[Int32]

0 to n - 1 in a random order: the indices of a shuffle, without the array to shuffle. A negative n gives an empty array.

pub fn[R : @contract.Rng] poisson(r : R, mu : Double) -> Int64

A Poisson draw with mean mu: multiplication for small means, and the transformed-rejection method with a squeeze for large ones, so that a mean of a million costs the same as a mean of a hundred.

pub fn[R : @contract.Rng] randbytes(r : R, n : Int32) -> Bytes

n bytes from the stream, the way fill writes them into an array you already have. A negative count gives an empty Bytes.

pub fn[R : @contract.Rng] randint(r : R, lo : Int64, hi : Int64) -> Int64

A uniform integer in the closed interval [lo, hi], unbiased.

pub fn[R : @contract.Rng] random(r : R) -> Double

A uniform Double in [0, 1), with 53 bits of resolution, as Python's random.random draws it.

pub fn[R : @contract.Rng] randrange(r : R, stop : Int64, start~ : Int64 = 0, step? : Int64) -> Int64

A uniform integer from range(start, stop, step), as Python's randrange. stop is required; start defaults to 0 and step to 1.

pub fn[R : @contract.Rng] rayleigh(r : R, scale~ : Double = 1.0) -> Double

A Rayleigh draw with scale scale: the length of a two-dimensional normal vector whose components have that standard deviation.

pub fn[R : @contract.Rng, T] sample(r : R, items : ArrayView[T], k : Int32, weights? : ArrayView[Double]) -> Array[T]

k distinct elements, by a Fisher-Yates partial shuffle. With weights, each place is filled from what is still left, in proportion to the weight of each remaining element, so a heavier element tends to come out earlier.

pub fn seeded(BytesView) -> @chacha.ChaCha12

Start from thirty-two bytes. The same bytes give the same stream, bit for bit on all four backends.

pub fn[R : @contract.Rng, T] shuffle(r : R, a : Array[T]) -> Unit

Shuffle a in place, Fisher-Yates.

pub fn[R : @contract.Rng] student_t(r : R, nu : Double) -> Double

A Student's t draw with nu degrees of freedom: a normal draw over the square root of a chi-squared draw divided by its own degrees of freedom.

pub fn[R : @contract.Rng] triangular(r : R, low~ : Double = 0.0, high~ : Double = 1.0, mode? : Double) -> Double

A triangular draw between low and high with the peak at mode, which defaults to the midpoint. The inverse of the triangular CDF, folded so that one uniform does the work.

pub fn[R : @contract.Rng] uniform(r : R, a : Double, b : Double) -> Double

A uniform Double in [a, b).

pub fn[R : @contract.Rng] vonmisesvariate(r : R, mu : Double, kappa : Double) -> Double

A von Mises draw with mean angle mu and concentration kappa, by the Best-Fisher rejection scheme Python's vonmisesvariate uses. Angles come back in [0, 2 pi).

pub fn[R : @contract.Rng] weibullvariate(r : R, alpha : Double, beta : Double) -> Double

A Weibull draw with scale alpha and shape beta, as Python's weibullvariate names them.

type aliases

pub type StdRng = @chacha.ChaCha12 ///| /// A `StdRng` seeded from `seed`, reproducible for the same seed. pub fn seeded(seed : BytesView) -> StdRng

The generator contract lives in the contract subpackage so the root package and the algorithm subpackages can both use it without a circular import. To name the traits, import @moonrand/contract (Rng, Secure, Seeded); the root package adds the convenience names below.

The default generator is ChaCha12 (see StdRng). Two ways in: seeded is pure and reproduces a stream from a seed you supply; @auto.rng() asks the platform through moonseed for a seed no one can predict or replay. Everything else in the library is pure, so a seeded run repeats exactly.

The default generator: ChaCha12. Change this one line to change the whole library's default algorithm.

02 / the contract

moonbitstack/moonrand/contract8 items

What a generator is, held apart so every package can share it.

traits

Rng

trait
pub(open) trait Rng

The contract every generator satisfies: produce uniform machine words. The algorithms in subpackages implement the one required method next_u64; the other two are shared defaults. The sampling and distribution functions in moonrand are generic over any Rng.

Methods take Self by value and keep their state in interior-mutable fields (Array buffers and priv mut scalars), so a draw advances the caller's generator without a mutable reference. That is also why a generator is a handle rather than a value: let b = a copies the handle and shares its buffer, so both draws advance one state. For two independent streams, build two generators from the seed.

fn next_u64(Self) -> UInt64

The contract's draw: one 64-bit word.

fn next_u32(Self) -> UInt32 = _

The contract's draw: one 32-bit word.

fn fill(Self, FixedArray[Byte]) -> Unit = _

Fill a byte array from the stream.

Secure

trait
pub(open) trait Secure : Rng {} ///| /// A generator rebuilt from a seed, for reproducible runs. pub(open) trait Seeded : Rng

A generator fit for cryptography: ChaCha and the like. Philox and PCG pass statistical tests but are not, so they do not carry this marker.

Seeded

trait
pub(open) trait Seeded : Rng

A contract a generator keeps.

fn from_seed(BytesView) -> Self

Rebuild from a seed of seed_size bytes.

fn seed_size() -> Int32

How many bytes a seed is.

03 / entropy

moonbitstack/moonrand/auto1 item

One call, seeded from the platform through moonseed.

functions

pub fn rng() -> @moonrand.StdRng raise @moonseed.Unavailable

A StdRng seeded from the platform's entropy: 32 bytes asked of @system.source through moonseed, then handed to @moonrand.seeded.

This is the only place the library touches the platform. It fails with @moonseed.Unavailable when there is no entropy source or when reading it fails; there is no fallback to a fixed or clock-derived seed, because a silent fallback would hand out a generator that looks random and is not.

test {
let r = @auto.rng() catch { _ => return }
let x = @moonrand.random(r)
assert_true(x >= 0.0 && x < 1.0)
}

04 / the bit helpers

moonbitstack/moonrand/bits4 items

Word arithmetic the generators share: a finalizer, a wide multiply, seed expansion.

functions

pub fn mix64(z : UInt64) -> UInt64

Arithmetic wider than a machine word, and the one way a byte seed becomes state words. Both are shared: 128-bit generators need the high half of a 64-bit product, and every generator needs the same expansion so the same seed means the same stream in every subpackage.

The SplitMix64 finalizer: three xorshifts and two odd multipliers, a bijection on 64-bit words.

pub fn mul_hi(a : UInt64, b : UInt64) -> UInt64

The high 64 bits of a * b, without a 128-bit type: split both into 32-bit halves and carry the middle terms.

pub fn mul_wide(a : UInt64, b : UInt64) -> (UInt64, UInt64)

Both halves of a * b, the low one first.

pub fn seed_words(seed : BytesView, n : Int32) -> FixedArray[UInt64]

n state words from seed. The seed is read eight bytes at a time, little endian, mixed into a SplitMix64 counter, and the counter is run forward — so every byte of the seed counts, and a short seed is the same as itself with zero bytes appended.

05 / stream ciphers

moonbitstack/moonrand/chacha27 items

ARX ciphers run as generators; the twelve-round form is the default.

types

ChaCha12

Rng, Secure, Seeded
pub struct ChaCha12

Twelve rounds of ChaCha: the default generator, StdRng.

advance(self : ChaCha12, n : UInt64) -> Unit

Skip the next n 64-bit draws.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> ChaCha12

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> ChaCha12

Seed it: the bytes go through SplitMix64, a short seed padded.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_nonce(seed : BytesView, nonce0 : UInt32, nonce1 : UInt32, nonce2 : UInt32, ctr : UInt32) -> ChaCha12

Start from a seed, a 96-bit nonce in three words, and a block counter: one key can feed several streams that never meet.

ChaCha20

Rng, Secure, Seeded
pub struct ChaCha20

Twenty rounds of ChaCha, the count RFC 8439 specifies.

advance(self : ChaCha20, n : UInt64) -> Unit

Skip the next n 64-bit draws.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> ChaCha20

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> ChaCha20

Seed it: the bytes go through SplitMix64, a short seed padded.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_nonce(seed : BytesView, nonce0 : UInt32, nonce1 : UInt32, nonce2 : UInt32, ctr : UInt32) -> ChaCha20

Start from a seed, a 96-bit nonce in three words, and a block counter: one key can feed several streams that never meet.

ChaCha8

Rng, Secure, Seeded
pub struct ChaCha8

ChaCha: a 256-bit-key stream cipher, used here as a generator. The three variants differ only in round count; the 12-round form is the library default. The cipher follows RFC 8439 exactly (32-bit block counter in state word 12, 96-bit nonce in words 13-15); gen_vectors checks its keystream against the RFC test vectors.

State lives in interior-mutable Array buffers and a priv mut counter, so next_u64 advances the generator through a by-value Self.

advance(self : ChaCha8, n : UInt64) -> Unit

Skip the next n 64-bit draws. The counter names the block, so this is a seek rather than a march: advance costs the same for one draw as for 2^40.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> ChaCha8

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> ChaCha8

Seed it: the bytes go through SplitMix64, a short seed padded.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_nonce(seed : BytesView, nonce0 : UInt32, nonce1 : UInt32, nonce2 : UInt32, ctr : UInt32) -> ChaCha8

Start from a seed, a 96-bit nonce in three words, and a block counter: one key can feed several streams that never meet.

06 / chaotic maps

moonbitstack/moonrand/chaos32 items

Logistic, tent, sine and Chebyshev maps: for looks, not for quality.

types

Chebyshev

Rng, Seeded
pub struct Chebyshev

Chebyshev: x := cos(n acos(x)), chaotic for n at or above 2.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Chebyshev

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Chebyshev

A generator seeded from eight bytes, at n = 3, mapped into (-1, 1).

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(n : Double, x : Double) -> Chebyshev

A generator with the degree n and the starting point x in (-1, 1).

Logistic

Rng, Seeded
pub struct Logistic

Logistic: x := r x (1 - x). The parameters that give a full-looking orbit are r near 4; the map is chaotic there and nowhere else.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Logistic

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Logistic

A generator seeded from eight bytes, at r = 4.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(r : Double, x : Double) -> Logistic

A generator with the parameter r and the starting point x.

Sine

Rng, Seeded
pub struct Sine

Sine: x := sin(pi x), which is chaotic on (0, 1) and never leaves it.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Sine

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Sine

A generator seeded from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(x : Double) -> Sine

A generator over the starting point x in (0, 1).

Tent

Rng, Seeded
pub struct Tent

Tent: x := mu x when x < 1/2, mu (1 - x) otherwise; mu near 2 is the chaotic case.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Tent

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Tent

A generator seeded from eight bytes, at mu = 2.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(mu : Double, x : Double) -> Tent

A generator with the parameter mu and the starting point x.

07 / the old ones

moonbitstack/moonrand/classic40 items

Middle square and its descendants, up to Blum Blum Shub.

types

Bbs

Rng, Seeded
pub struct Bbs

Bbs: Blum Blum Shub, the square modulo the product of two primes. The generator is provably hard to predict from its low bits under a factoring assumption — and slow, which is why it stays a cipher-flavoured footnote. This one works modulo a 32-bit modulus, so a word square fits in a double word; the modulus must be a product of two primes congruent to 3 mod 4, or the period argument does not hold.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Bbs

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Bbs

A generator from eight bytes, over 383 * 503 = 192649, a Blum integer that fits a word.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(modulus : UInt64, seed : UInt64) -> Bbs

A generator over seed modulo modulus.

CollatzWeyl

Rng, Seeded
pub struct CollatzWeyl

CollatzWeyl: the Collatz map (halve when even, three n plus one when odd) mixed with a Weyl sequence. The Collatz map on its own is a conjecture, not a generator; the Weyl word is what carries the period, and the xor is what hides the map's structure in the low bits.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> CollatzWeyl

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> CollatzWeyl

A generator from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> CollatzWeyl

A generator over the starting state, with the Weyl word at zero.

MiddleProduct

Rng, Seeded
pub struct MiddleProduct

MiddleProduct: multiply by a fixed constant and keep the middle bits, the mid-product method of the same era.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> MiddleProduct

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MiddleProduct

A generator from eight bytes, with the golden-ratio multiplier.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(multiplier : UInt64, seed : UInt64) -> MiddleProduct

A generator with multiplier and the starting seed.

MiddleSquare

Rng, Seeded
pub struct MiddleSquare

MiddleSquare: square the word and keep the middle thirty-two bits, as von Neumann described it. A zero state is absorbing, so a zero seed is moved off it; even so, the period can collapse, which is why this is here as a historical entry rather than as a default.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> MiddleSquare

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MiddleSquare

A generator from eight bytes.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> MiddleSquare

A generator over state, with zero replaced by a constant.

MiddleSquareWeyl

Rng, Seeded
pub struct MiddleSquareWeyl

MiddleSquareWeyl: the middle-square word plus a Weyl sequence, which is what keeps it from collapsing into its own short cycle.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> MiddleSquareWeyl

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MiddleSquareWeyl

A generator from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> MiddleSquareWeyl

A generator over the starting state, with the Weyl word at zero.

08 / lagged Fibonacci

moonbitstack/moonrand/fib48 items

Additive and subtractive lags, with and without carry, and RANLUX.

types

AddWithCarry

Rng, Seeded
pub struct AddWithCarry

AddWithCarry: Marsaglia's add-with-carry over 55 words, the carry kept across draws so that the recurrence is not linear modulo 2^32.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> AddWithCarry

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> AddWithCarry

A generator over the 55 state words, with the carry clear.

new(seed : BytesView) -> AddWithCarry

A generator from 55 words of SplitMix64 output.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

AdditiveCongruential

Rng, Seeded
pub struct AdditiveCongruential

Lagged Fibonacci generators and the carry forms of them. They are cheap and they pass the tests, but they are linear over the words they add, which is why the ranlux variants throw most of their output away.

AdditiveCongruential: x(n) = x(n-55) + x(n-24) modulo 2^32, the lagged Fibonacci generator of the numerical recipes.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> AdditiveCongruential

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> AdditiveCongruential

A generator over the 55 state words.

new(seed : BytesView) -> AdditiveCongruential

A generator from 55 words of SplitMix64 output, none of them all zero.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

LaggedFibonacci

Rng, Seeded
pub struct LaggedFibonacci

LaggedFibonacci: the same recurrence with lags 607 and 273, for a period that does not fit in a demo.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> LaggedFibonacci

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> LaggedFibonacci

A generator over the 607 state words.

new(seed : BytesView) -> LaggedFibonacci

A generator from 607 words of SplitMix64 output.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Ranlux24

Rng, Seeded
pub struct Ranlux24

Ranlux24: subtract-with-borrow over 24-bit words, 24 of them with a lag of ten, and all but one in 223 of each block thrown away. The discard is what turns a weak linear recurrence into something that passes the tests.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Ranlux24

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Ranlux24

A generator over the 24 state words.

new(seed : BytesView) -> Ranlux24

A generator from eight bytes, seeded the way the reference does.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Ranlux48

Rng, Seeded
pub struct Ranlux48

Ranlux48: the same generator over 48-bit words, twelve of them with a lag of five, discarding 389 of every 390.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Ranlux48

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Ranlux48

A generator over the twelve state words.

new(seed : BytesView) -> Ranlux48

A generator from eight bytes, seeded the way the reference does.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

SubtractWithBorrow

Rng, Seeded
pub struct SubtractWithBorrow

SubtractWithBorrow: the same shape with a borrow, over 55 words. The borrow is what makes the state more than the sum of its words.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> SubtractWithBorrow

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> SubtractWithBorrow

A generator over the 55 state words, with the borrow clear.

new(seed : BytesView) -> SubtractWithBorrow

A generator from 55 words of SplitMix64 output.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

09 / JSF

moonbitstack/moonrand/jsf8 items

Jenkins' small fast generator, a rotate per draw.

types

Jsf64

Rng, Seeded
pub struct Jsf64

JSF64: Jenkins' small fast generator — four words, one rotate per step, and a guaranteed period of at least 2^64.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Jsf64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Jsf64

A generator seeded from 32 bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(a : UInt64, b : UInt64, c : UInt64, d : UInt64) -> Jsf64

A generator over the four state words.

10 / linear and quadratic

moonbitstack/moonrand/lcg50 items

Congruential recurrences: linear, quadratic, cubic, inversive.

types

Ccg64

Rng, Seeded
pub struct Ccg64

Ccg64: a cubic congruential generator, x := a x^3 + b x^2 + c modulo 2^64, rotated by half a word as it leaves.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Ccg64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Ccg64

A generator from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(a : UInt64, b : UInt64, c : UInt64, seed : UInt64) -> Ccg64

A generator with the three coefficients and the starting seed.

Icg32

Rng, Seeded
pub struct Icg32

Icg32: an inversive congruential generator, x := a x^-1 + c modulo the prime 2^32-5. Unlike every other generator here the map is not linear, so the low bits are as good as the high ones.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Icg32

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Icg32

A generator from eight bytes.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(multiplier : UInt64, increment : UInt64, seed : UInt64) -> Icg32

A generator with multiplier, increment and the starting seed, all taken modulo the prime.

Lcg48

Rng, Seeded
pub struct Lcg48

Lcg48: the 48-bit generator behind drand48, x := 0x5DEECE66D x + 0xB modulo 2^48.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Lcg48

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Lcg48

A generator from eight bytes, as srand48 takes them.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> Lcg48

A generator over state, of which only the low 48 bits count.

Lcg64

Rng, Seeded
pub struct Lcg64

Congruential generators, linear and polynomial, plus the inversive one: the oldest family, kept because their periods and their faults are both exactly known.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Lcg64

Rebuild from a seed of seed_size bytes.

glibc(seed : UInt64) -> Lcg64

The parameters the C library and glibc use.

new(seed : BytesView) -> Lcg64

A generator from eight bytes, with the Numerical Recipes parameters.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

numerical_recipes(seed : UInt64) -> Lcg64

The parameters Numerical Recipes prints.

seed_size() -> Int32

How many bytes a seed is.

with_params(multiplier : UInt64, increment : UInt64, seed : UInt64) -> Lcg64

A generator with multiplier, increment and the starting seed: the recurrence is x := multiplier * x + increment modulo 2^64.

MinStd

Rng, Seeded
pub struct MinStd

MinStd: the minimal standard generator, x := 16807 x modulo 2^31-1, the multiplicative one Park and Miller recommended.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> MinStd

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MinStd

A generator from eight bytes, mapped into the usable range.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> MinStd

A generator over state, which must be in [1, 2^31-2].

Qcg64

Rng, Seeded
pub struct Qcg64

Qcg64: a quadratic congruential generator, x := a x^2 + c modulo 2^64. The state is rotated by half a word before it leaves, because the low bits of a polynomial recurrence are the weak ones.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Qcg64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Qcg64

A generator from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_params(multiplier : UInt64, increment : UInt64, seed : UInt64) -> Qcg64

A generator with multiplier and increment (both odd is the useful choice) and the starting seed.

11 / LFSR

moonbitstack/moonrand/lfsr40 items

Galois and Tausworthe registers, and L'Ecuyer's combined forms.

types

Gfsr

Rng, Seeded
pub struct Gfsr

Gfsr: a generalized feedback shift register of 250 words, each new word the xor of itself and the word 103 places back — Lewis and Payne's parameters.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Gfsr

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Gfsr

A generator over the 250 state words, starting at index zero.

new(seed : BytesView) -> Gfsr

A generator from 250 words of SplitMix64 output.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Lfsr113

Rng, Seeded
pub struct Lfsr113

Lfsr113: four combined linear feedback registers, period near 2^113.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Lfsr113

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Lfsr113

A generator from sixteen bytes through SplitMix64, each component moved above 1, 7, 15 and 127 respectively.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(s1 : UInt32, s2 : UInt32, s3 : UInt32, s4 : UInt32) -> Lfsr113

A generator over the four component states.

Lfsr258

Rng, Seeded
pub struct Lfsr258

Lfsr258: five combined linear feedback registers over 64-bit words, period near 2^258. The shifts and masks are L'Ecuyer's; each component keeps its low bits clear so that its recurrence has the stated period.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Lfsr258

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Lfsr258

A generator from forty bytes through SplitMix64, each component moved into its admissible range.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(s1 : UInt64, s2 : UInt64, s3 : UInt64, s4 : UInt64, s5 : UInt64) -> Lfsr258

A generator over the five component states.

Lfsr64

Rng, Seeded
pub struct Lfsr64

Shift-register generators: one LFSR, one generalized feedback register, and the combined forms L'Ecuyer built out of several. A single LFSR is linear — its next bit is a linear function of the state — which is what the combined forms exist to hide.

Lfsr64: a Galois register over the maximal-period polynomial x^64 + x^63 + x^61 + x^60.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Lfsr64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Lfsr64

A generator from eight bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> Lfsr64

A generator over state, which must not be zero: zero is the fixed point of every shift register.

Taus88

Rng, Seeded
pub struct Taus88

Taus88: three Tausworthe registers combined by xor, L'Ecuyer's 1996 generator; the periods multiply out to about 2^88.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Taus88

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Taus88

A generator from twelve bytes through SplitMix64, each component moved into its admissible range: above 1, 7 and 15 respectively.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(s1 : UInt32, s2 : UInt32, s3 : UInt32) -> Taus88

A generator over the three component states.

12 / LXM

moonbitstack/moonrand/lxm18 items

A congruential generator added to a xoshiro one, as Blackman and Vigna mix them.

types

L64X128

Rng, Seeded
pub struct L64X128

L64X128: a 64-bit LCG over xoroshiro128+.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> L64X128

Rebuild from a seed of seed_size bytes.

jump(self : L64X128) -> Unit

Skip 2^128 draws: both halves move together, or the sum stops meaning anything.

new(seed : BytesView) -> L64X128

A generator from twenty-four bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(lcg : UInt64, s0 : UInt64, s1 : UInt64) -> L64X128

A generator over the LCG state lcg and the xoroshiro state s0, s1.

L64X256

Rng, Seeded
pub struct L64X256

L64X256: the same LCG over xoshiro256+.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> L64X256

Rebuild from a seed of seed_size bytes.

jump(self : L64X256) -> Unit

Skip 2^128 draws, both halves together: the xoshiro half takes its own 2^128 jump once, the LCG half squares its affine map 128 times.

new(seed : BytesView) -> L64X256

A generator from forty bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(lcg : UInt64, s0 : UInt64, s1 : UInt64, s2 : UInt64, s3 : UInt64) -> L64X256

A generator over the LCG state lcg and the xoshiro state s0 to s3.

13 / multiple recurrences

moonbitstack/moonrand/mrg9 items

L'Ecuyer's MRG32k3a: two cubic recurrences over primes.

types

Mrg32k3a

Rng, Seeded
pub struct Mrg32k3a

A generator.

advance(self : Mrg32k3a, n : UInt64) -> Unit

Skip n draws by stepping the recurrence n times. Each component is a linear recurrence, so a matrix power would do this in log n steps; the loop is what is here, and the doc says so rather than pretending otherwise.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Mrg32k3a

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[Int64]) -> Mrg32k3a

A generator over the six reference state words, the first three for the first component.

new(seed : BytesView) -> Mrg32k3a

A generator from twenty-four bytes through SplitMix64, each component reduced into its own prime.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

14 / Mersenne Twister

moonbitstack/moonrand/mt27 items

The 19937 pair and its SIMD-oriented form.

types

MT19937

Rng, Seeded
pub struct MT19937

MT19937: the 32-bit Mersenne Twister, period 2^19937-1, 624 words of state and an index into them. The seeding, the twist and the tempering are the reference init_genrand, init_by_array and genrand_int32.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_array(keys : FixedArray[UInt32]) -> MT19937

The reference seeding init_by_array: a key of any length, mixed in.

from_seed(seed : BytesView) -> MT19937

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MT19937

A generator from a byte seed: four bytes at a time, little endian, handed to from_array. A seed shorter than four bytes is zero-padded.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_word(seed : UInt32) -> MT19937

The reference seeding init_genrand: one word, expanded by the 1812433253 recursion.

MT19937_64

Rng, Seeded
pub struct MT19937_64

MT19937-64: the 64-bit Mersenne Twister, period 2^19937-1, 312 words.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_array(keys : FixedArray[UInt64]) -> MT19937_64

The reference seeding init_by_array64.

from_seed(seed : BytesView) -> MT19937_64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> MT19937_64

A generator from a byte seed, eight bytes at a time, little endian.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_word(seed : UInt64) -> MT19937_64

The reference seeding init_genrand64.

Sfmt19937

Rng, Seeded
pub struct Sfmt19937

SFMT19937.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_array(keys : FixedArray[UInt32]) -> Sfmt19937

The reference seeding init_by_array: a key of any length, mixed through three passes over the state.

from_seed(seed : BytesView) -> Sfmt19937

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Sfmt19937

A generator from a byte seed, four bytes at a time, little endian.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_word(seed : UInt32) -> Sfmt19937

The reference seeding init_gen_rand: one word, expanded by the same 1812433253 recursion MT19937 uses, then certified.

15 / PCG

moonbitstack/moonrand/pcg27 items

O'Neill's permuted congruential generators, 32 and 64 bits of output.

types

Pcg32

Rng, Seeded
pub struct Pcg32

PCG32: a 64-bit LCG, 32 bits of permuted output. The increment names the stream; two generators with different increments never meet.

advance(self : Pcg32, n : UInt64) -> Unit

Skip the next n draws of the underlying LCG, by stepping the state n times without asking for the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Pcg32

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Pcg32

A generator from sixteen bytes: eight for the state, eight for the stream.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64, inc : UInt64) -> Pcg32

A generator over the LCG state state and the stream inc. The reference makes inc odd, and so does this.

with_stream(seed : UInt64, stream : UInt64) -> Pcg32

The reference pcg32_srandom seeding: stream names the sequence and seed picks the place in it, so two streams never overlap.

Pcg64

Rng, Seeded
pub struct Pcg64

PCG64: a 128-bit LCG with the XSL-RR output, the permutation NumPy's default generator uses.

advance(self : Pcg64, n : UInt64) -> Unit

Skip the next n draws of the underlying LCG, by stepping the state n times without asking for the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Pcg64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Pcg64

A generator from thirty-two bytes: sixteen of state, sixteen of stream.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state_lo : UInt64, state_hi : UInt64, inc_lo : UInt64, inc_hi : UInt64) -> Pcg64

A generator over the 128-bit state state_lo + state_hi and the 128-bit stream inc_lo + inc_hi.

Pcg64Dxsm

Rng, Seeded
pub struct Pcg64Dxsm

PCG64-DXSM: a 128-bit LCG with the double-xorshift-multiply output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Pcg64Dxsm

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Pcg64Dxsm

A generator from thirty-two bytes: sixteen of state, sixteen of stream.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state_lo : UInt64, state_hi : UInt64, inc_lo : UInt64, inc_hi : UInt64) -> Pcg64Dxsm

A generator over the 128-bit state and the 128-bit stream.

16 / counter based

moonbitstack/moonrand/philox18 items

Random123's Philox: a bijection over counter words, no state to carry.

types

Philox4x32

Rng, Seeded
pub struct Philox4x32

Philox: a counter-based generator. The state is a counter and a key, the round is a multiply-high-low and a pair of xors, and there is no feedback — which is why a Philox stream can be split across threads or machines by handing each of them a different counter.

The round constants and the key schedule are the ones in the reference implementation the algorithm was published with.

advance(self : Philox4x32, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Philox4x32

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Philox4x32

A generator keyed from eight bytes.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt32, k1 : UInt32) -> Philox4x32

A generator with the key k0, k1 and a zero counter.

Philox4x64

Rng, Seeded
pub struct Philox4x64

Philox4x64: the same round over 64-bit words, with the 128-bit multiplier split into its two halves.

advance(self : Philox4x64, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Philox4x64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Philox4x64

A generator keyed from sixteen bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt64, k1 : UInt64) -> Philox4x64

A generator with the key k0, k1 and a zero counter.

17 / SFC

moonbitstack/moonrand/sfc8 items

Chris Doty-Humphrey's chaotic generator: three words, a counter, a carry.

types

Sfc64

Rng, Seeded
pub struct Sfc64

SFC64: three words of state and a counter, mixed by addition and shifts. The counter makes the period at least 2^64 whatever the state; the shift constants are the author's.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Sfc64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Sfc64

A generator seeded from 24 bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(a : UInt64, b : UInt64, c : UInt64) -> Sfc64

A generator over the state words a, b, c, with the counter at zero.

18 / SplitMix

moonbitstack/moonrand/splitmix9 items

A counter through a finalizer: the seed expander, as a generator.

types

SplitMix64

Rng, Seeded
pub struct SplitMix64

SplitMix64: a counter, a golden-ratio step and the finalizer in bits. Period 2^64, one 64-bit word of state. It is the generator the xoshiro family is seeded from, and a generator in its own right.

advance(self : SplitMix64, n : UInt64) -> Unit

Skip the next n draws by adding n golden-ratio steps to the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> SplitMix64

Rebuild from a seed of seed_size bytes.

from_state(state : UInt64) -> SplitMix64

A generator holding state as its counter.

new(seed : BytesView) -> SplitMix64

A generator seeded from eight bytes; a shorter seed is zero-padded.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

19 / squares

moonbitstack/moonrand/squares9 items

Widynski's Squares, and the middle-square family it descends from.

types

Squares

Rng, Seeded
pub struct Squares

Squares: a counter-based generator with a 256-bit key and a 256-bit counter. The published family of that name is parametrised by how many words it mixes and how many rounds of squaring it takes; this is the three-word three-round shape, with a cross add between the squarings so that a word depends on its neighbours.

The construction here is the one this library defines — the counter and the key are the published shape, the round function is written out below — and the tests replay what scripts/gen_vectors.go produces from the same definition. It is not claimed to match the author's own constants word for word.

advance(self : Squares, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Squares

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Squares

A generator from thirty-two bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt64, k1 : UInt64, k2 : UInt64, k3 : UInt64) -> Squares

A generator with the four-word key k0 to k3 and a zero counter.

20 / counter based

moonbitstack/moonrand/threefry27 items

Random123's Threefry: the same idea without a multiply.

types

Threefry2x64

Rng, Seeded
pub struct Threefry2x64

Threefry2x64: two 64-bit counter words, thirteen rounds, its own rotation schedule.

advance(self : Threefry2x64, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Threefry2x64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Threefry2x64

A generator keyed from sixteen bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt64, k1 : UInt64) -> Threefry2x64

A generator with the key k0, k1 and a zero counter.

Threefry4x32

Rng, Seeded
pub struct Threefry4x32

Threefry4x32: the same schedule over 32-bit words.

advance(self : Threefry4x32, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Threefry4x32

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Threefry4x32

A generator keyed from eight bytes.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt32, k1 : UInt32) -> Threefry4x32

A generator with the key k0, k1 and a zero counter.

Threefry4x64

Rng, Seeded
pub struct Threefry4x64

Threefry4x64: four 64-bit counter words, two 64-bit key words, twenty rounds.

advance(self : Threefry4x64, n : UInt64) -> Unit

Skip n 64-bit draws by stepping the counter.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Threefry4x64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Threefry4x64

A generator keyed from sixteen bytes.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_key(k0 : UInt64, k1 : UInt64) -> Threefry4x64

A generator with the key k0, k1 and a zero counter.

21 / WELL

moonbitstack/moonrand/well16 items

Well-equidistributed long-period registers, 512 and 1024 bits.

types

Well1024

Rng, Seeded
pub struct Well1024

WELL1024a: thirty-two 32-bit words, period 2^1024-1, the same construction as WELL512a with a longer ring. The tempering mask differs per instance and is the one the WELL reference gives for 1024a.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Well1024

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Well1024

A generator over thirty-two state words, starting at index zero.

new(seed : BytesView) -> Well1024

A generator seeded from 128 bytes through SplitMix64.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Well512

Rng, Seeded
pub struct Well512

WELL512a: sixteen 32-bit words and an index, period 2^512-1. The recursion is the reference WELLRNG512a, down to the mask on the temepering shift.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Well512

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Well512

A generator over sixteen state words, starting at index zero.

new(seed : BytesView) -> Well512

A generator seeded from 64 bytes through SplitMix64.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

22 / xorshift

moonbitstack/moonrand/xorshift40 items

Marsaglia's shifts, from 32 bits of state to XORWOW.

types

Xorshift128

Rng, Seeded
pub struct Xorshift128

Xorshift128: the four-word form of the 2003 paper.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xorshift128

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Xorshift128

A generator over the four state words x, y, z, w.

new(seed : BytesView) -> Xorshift128

A generator seeded from sixteen bytes through SplitMix64.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xorshift128Plus

Rng, Seeded
pub struct Xorshift128Plus

Xorshift128Plus: two 64-bit words, their sum as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xorshift128Plus

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Xorshift128Plus

A generator seeded from sixteen bytes through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(s0 : UInt64, s1 : UInt64) -> Xorshift128Plus

A generator over the two state words, which must not both be zero.

Xorshift32

Rng, Seeded
pub struct Xorshift32

Marsaglia's xorshift generators: the 2003 paper's 32-, 64- and 128-bit forms, the 128-bit sum, and XORWOW.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xorshift32

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Xorshift32

A generator seeded from four bytes; a zero seed becomes one.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt32) -> Xorshift32

A generator over state, which must not be zero.

Xorshift64

Rng, Seeded
pub struct Xorshift64

A generator.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xorshift64

Rebuild from a seed of seed_size bytes.

new(seed : BytesView) -> Xorshift64

A generator seeded from eight bytes; a zero seed becomes one.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

with_state(state : UInt64) -> Xorshift64

A generator over state, which must not be zero.

Xorwow

Rng, Seeded
pub struct Xorwow

Xorwow: xorshift128 with a Weyl increment added to the output, Marsaglia's contribution to the Curand library.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xorwow

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt32]) -> Xorwow

A generator over the five state words x, y, z, w, v and the Weyl word d.

new(seed : BytesView) -> Xorwow

A generator seeded from 24 bytes through SplitMix64.

next_u32(self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

23 / xoshiro

moonbitstack/moonrand/xoshiro90 items

Vigna's shift-register family, 128 and 256 and 512 bits of state.

types

Xoroshiro128p

Rng, Seeded
pub struct Xoroshiro128p

xoroshiro128+: two words of state, an addition as the output; small and fast, and the weakest of the family in the low bits.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoroshiro128p

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoroshiro128p

A generator whose state is s, taken as given: 2 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoroshiro128p) -> Unit

Skip 2^64 draws.

long_jump(self : Xoroshiro128p) -> Unit

Skip 2^96 draws.

new(seed : BytesView) -> Xoroshiro128p

A generator seeded from 16 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoroshiro128pp

Rng, Seeded
pub struct Xoroshiro128pp

xoroshiro128++: two words of state, a rotated sum as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoroshiro128pp

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoroshiro128pp

A generator whose state is s, taken as given: 2 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoroshiro128pp) -> Unit

Skip 2^64 draws.

long_jump(self : Xoroshiro128pp) -> Unit

Skip 2^96 draws.

new(seed : BytesView) -> Xoroshiro128pp

A generator seeded from 16 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoroshiro128ss

Rng, Seeded
pub struct Xoroshiro128ss

xoroshiro128\*\*: two words of state, a scrambled product as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoroshiro128ss

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoroshiro128ss

A generator whose state is s, taken as given: 2 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoroshiro128ss) -> Unit

Skip 2^64 draws.

long_jump(self : Xoroshiro128ss) -> Unit

Skip 2^96 draws.

new(seed : BytesView) -> Xoroshiro128ss

A generator seeded from 16 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro256p

Rng, Seeded
pub struct Xoshiro256p

xoshiro256+: a 256-bit state and an addition as the output; the fastest of the three when all that is wanted is a fraction from the high bits.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro256p

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro256p

A generator whose state is s, taken as given: 4 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro256p) -> Unit

Skip 2^128 draws.

long_jump(self : Xoshiro256p) -> Unit

Skip 2^192 draws.

new(seed : BytesView) -> Xoshiro256p

A generator seeded from 32 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro256pp

Rng, Seeded
pub struct Xoshiro256pp

xoshiro256++: a 256-bit state and a rotated sum as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro256pp

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro256pp

A generator whose state is s, taken as given: 4 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro256pp) -> Unit

Skip 2^128 draws.

long_jump(self : Xoshiro256pp) -> Unit

Skip 2^192 draws.

new(seed : BytesView) -> Xoshiro256pp

A generator seeded from 32 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro256ss

Rng, Seeded
pub struct Xoshiro256ss

xoshiro256\*\*: a 256-bit state and a scrambled product as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro256ss

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro256ss

A generator whose state is s, taken as given: 4 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro256ss) -> Unit

Skip 2^128 draws.

long_jump(self : Xoshiro256ss) -> Unit

Skip 2^192 draws.

new(seed : BytesView) -> Xoshiro256ss

A generator seeded from 32 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro512p

Rng, Seeded
pub struct Xoshiro512p

xoshiro512+: a 512-bit state and an addition as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro512p

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro512p

A generator whose state is s, taken as given: 8 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro512p) -> Unit

Skip 2^256 draws.

long_jump(self : Xoshiro512p) -> Unit

Skip 2^384 draws.

new(seed : BytesView) -> Xoshiro512p

A generator seeded from 64 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro512pp

Rng, Seeded
pub struct Xoshiro512pp

xoshiro512++: a 512-bit state and a rotated sum as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro512pp

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro512pp

A generator whose state is s, taken as given: 8 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro512pp) -> Unit

Skip 2^256 draws.

long_jump(self : Xoshiro512pp) -> Unit

Skip 2^384 draws.

new(seed : BytesView) -> Xoshiro512pp

A generator seeded from 64 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.

Xoshiro512ss

Rng, Seeded
pub struct Xoshiro512ss

xoshiro512\*\*: a 512-bit state and a scrambled product as the output.

fill(Self, FixedArray[Byte]) -> Unit

Fill a byte array from the stream.

from_seed(seed : BytesView) -> Xoshiro512ss

Rebuild from a seed of seed_size bytes.

from_state(s : FixedArray[UInt64]) -> Xoshiro512ss

A generator whose state is s, taken as given: 8 words, no expansion, so a published state reproduces the published stream.

jump(self : Xoshiro512ss) -> Unit

Skip 2^256 draws.

long_jump(self : Xoshiro512ss) -> Unit

Skip 2^384 draws.

new(seed : BytesView) -> Xoshiro512ss

A generator seeded from 64 bytes, expanded through SplitMix64.

next_u32(Self) -> UInt32

The contract's draw: one 32-bit word.

next_u64(self) -> UInt64

The contract's draw: one 64-bit word.

seed_size() -> Int32

How many bytes a seed is.