flix

0.81.0

Rng

enum Rng with Eq, ToStringSource
case Rng(Int64)

A pseudo-random number generator, as a value.

Randomness is deliberately data rather than an effect. A generator is threaded through step like any other part of the world, which means a simulation that uses randomness is still a pure function of its inputs. Replaying a recorded input script from the same seed therefore produces a bit-identical result, with no handler to install and no shared generator to reason about across threads.

This is a 64-bit linear congruential generator using the multiplier and increment from Knuth's MMIX. It is not cryptographic and is not meant to be.

Instances

Eq, ToString

Definitions

def chance(oneIn: Int32, r: Rng): (Bool, Rng)§ Source

Returns true with probability 1 / oneIn, and the advanced generator.

def fromSeed(seed: Int64): Rng§ Source

Creates a generator from seed.

The state is advanced once so that adjacent seeds do not produce correlated first values, which is the usual failure mode of a bare LCG.

def nextInt32Below(bound: Int32, r: Rng): (Int32, Rng)§ Source

Returns a value in 0 ..< bound and the advanced generator.

A bound of zero or less yields 0, so callers never have to guard.

def nextInt64(r: Rng): (Int64, Rng)§ Source

Returns the next 64-bit value and the advanced generator.

def nextNonNegative(r: Rng): (Int64, Rng)§ Source

Returns a non-negative 64-bit value and the advanced generator.

Negatives are folded rather than negated, because negating Int64.minValue would overflow back to itself.