Random

:= []

PCG algorithms, constants, and wrappers

For more information about PCG see www.pcg-random.org

PCG is a family of simple fast space-efficient statistically good algorithms for random number generation.

seed : U32 -> State

Construct an initial "seed" State for Generators

seed_variant : U32, U32 -> State

Construct a specific "variant" of a "seed" State for more advanced use.

Takes a starting seed and a sequence ID (which corresponds to its internal update_increment). Any States with different sequence ID's will share no consecutive number pairs with each other, even if they are initialized with the same seed.

Any value given for the sequence ID between 0 and 2**31 will be unique. Above that, the sequence ID wraps around to the bottom, so there are about two billion unique choices for sequence ID

step : State, Generator(value) -> Generation(value)

Generate a Generation from a state

next : Generation(_), Generator(value) -> Generation(value)

Generate a new Generation from an old Generation's state

static : value -> Generator(value)

Create a Generator that always returns the same thing.

map : Generator(a), (a -> b) -> Generator(b)

Map over the value of a Generator.

map2 : Generator(a), Generator(b), (a, b -> c) -> Generator(c)

Compose two Generators into a single Generator.

This is the applicative operation used by Roc's record-builder syntax:

date_generator =
    {
        year: Random.bounded_i32(1, 2500),
        month: Random.bounded_i32(1, 12),
        day: Random.bounded_i32(1, 31),
    }.Random
chain : Generator(a), Generator(b), (a, b -> c) -> Generator(c)

Compose two Generators into a single Generator.

This is an alias for map2; record-builder syntax calls map2 directly.

list : Generator(a), U64 -> Generator(List(a))

Generate a list of random values.

generate_10_random_u8s : Generator(List(U8))
generate_10_random_u8s =
    Random.list(Random.u8, 10)
u8 : Generator(U8)

Construct a Generator for 8-bit unsigned integers NOTE: We are just taking the bottom 8 bits of the generated U32 value Some backing generators have worse statistical properties in the low-order bits and it would be wise to use the upper 8 bits instead, but according to the pcg paper (M.E. O'Neill) this backing generator has good statistical quality throughout all the bits (perhaps from the good high bits being rotated/shifted around etc)

bounded_u8 : U8, U8 -> Generator(U8)

Construct a Generator for 8-bit unsigned integers between two boundaries (inclusive)

i8 : Generator(I8)

Construct a Generator for 8-bit signed integers

bounded_i8 : I8, I8 -> Generator(I8)

Construct a Generator for 8-bit signed integers between two boundaries (inclusive)

u16 : Generator(U16)

Construct a Generator for 16-bit unsigned integers

bounded_u16 : U16, U16 -> Generator(U16)

Construct a Generator for 16-bit unsigned integers between two boundaries (inclusive)

i16 : Generator(I16)

Construct a Generator for 16-bit signed integers

bounded_i16 : I16, I16 -> Generator(I16)

Construct a Generator for 16-bit signed integers between two boundaries (inclusive)

u32 : Generator(U32)

Construct a Generator for 32-bit unsigned integers

bounded_u32 : U32, U32 -> Generator(U32)

Construct a Generator for 32-bit unsigned integers between two boundaries (inclusive)

i32 : Generator(I32)

Construct a Generator for 32-bit signed integers

bounded_i32 : I32, I32 -> Generator(I32)

Construct a Generator for 32-bit signed integers between two boundaries (inclusive)

Generator : State -> Generation(value)

A generator that produces pseudorandom values using the PCG algorithm.

rgb_generator : Generator({ red: U8, green: U8, blue: U8 })
rgb_generator =
    {
        red: Random.u8,
        green: Random.u8,
        blue: Random.u8,
    }.Random
Generation : { value : value, state : State }

A pseudorandom value, paired with its Generator's output state.

This is required to chain multiple calls together passing the updated state.

State

Random.State :: # (opaque)

Internal state for Generators