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.

Generator value

A generator that produces pseudorandom values using the PCG algorithm.

rgb_generator : Generator { red: U8, green: U8, blue: U8 }
rgb_generator =
    { Random.chain <-
        red: Random.u8,
        green: Random.u8,
        blue: Random.u8,
    }

Generation value

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

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

State

Internal state for Generators

seed : U32 -> State

Construct an initial "seed" State for Generators

seed_variant : U32, U32 -> State

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

A "seed" is an initial State for Generators.

A "variant" is a State that specifies a c.updateIncrement constant, to produce a sequence of internal values that shares no consecutive pairs with other variants of the same State.

Odd numbers are recommended for the update increment, to double the repetition period of sequences (by hitting odd values).

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.

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

Compose two Generators into a single Generator.

This works well with record builders:

date_generator =
    { Random.chain <-
        year: Random.int(1, 2500),
        month: Random.int(1, 12),
        day: Random.int(1, 31),
    }

list : Generator a, Int * -> 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

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)