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), (a -> Generator(b)) -> Generator(b)

Create a Generator by chaining one Generator with a function that returns a new Generator

generate_random_amount_of_random_u8s =
    Random.chain(Random.bounded_U64(1, 10), |count| Random.list(Random.u8, count))
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)
shuffle : List(a) -> Generator(List(a))

Given a List generate a shuffled version of that List

bool : Generator(Bool)

A Generator that generates Bool.True or Bool.False with equal probabilty

u8 : Generator(U8)

A Generator for the full range of 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)

A Generator for the full range of 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)

A Generator for the full range of 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)

A Generator for the full range of 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)

A Generator for the full range of 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)

A Generator for the full range of 32-bit signed integers

bounded_i32 : I32, I32 -> Generator(I32)

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

u64 : Generator(U64)

A Generator for the full range of 64-bit unsigned integers

bounded_u64 : U64, U64 -> Generator(U64)

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

i64 : Generator(I64)

A Generator for the full range of 64-bit signed integers

bounded_i64 : I64, I64 -> Generator(I64)

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

f32 : F32, F32 -> Generator(F32)

Generator for an F32 between two values excluding the high one It is generally recommended NOT to use this when your intention is to convert the floating point number to an integer due to potential rounding errors etc, prefer the bounded_* integer generators for those cases

f64 : F64, F64 -> Generator(F64)

Generator for an F64 between two values excluding the high one It is generally recommended NOT to use this when your intention is to convert the floating point number to an integer due to potential rounding errors etc, prefer the bounded_* integer generators for those cases

choice : a, List(a) -> Generator(a)

Creates a Generator that randomly chooses from a series of items. The first item is given explicitly as the first argument to ensure that there's always at least one item to choose from. See Random.choice_try for an alternative that checks for an empty List.

choice_try : List(a) -> Try(Generator(a), [ListWasEmpty])

Creates a Generator that randomly chooses an item from a List. Returns Err(ListWasEmpty) upon construction if given an empty List. See Random.choice for a version that cannot return an error.

choice_weighted : (a, F64), List((a, F64)) -> Generator(a)

Creates a Generator that randomly chooses from a series of items with an associated weight. Higher weight indicates a higher probability of selection. The weights don't need to add up to any particular value, probability is relative to the total sum of given weights. The first item is given explicitly as the first argument to ensure that there's always at least one item to choose from. See Random.choice_weighted_try for an alternative that checks for an empty List

choice_weighted_try : List((a, F64)) -> Try(Generator(a), [ListWasEmpty])

Creates a Generator that randomly chooses from a series of items with an associated weight. Higher weight indicates higher a higher probability of selection. The weights don't need to add up to any particular value, probability is relative to the total sum of given weights. Returns Err(ListWasEmpty) upon construction if given an empty List. See Random.choice_weighted for a version that cannot return an error.

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

fast_forward : State, U32 -> State

Manually step the random state forward by n steps. The sequence has a period of 2 to the 32 steps, and will wrap around to the beginning after that.

rewind : State, U32 -> State

Manually step the random state backward by n steps. Will wrap around to the end of the sequence when stepping backward from the "0th" state.