Random

Core

Pseudo-random generators for the BEAM target.

Same Generator / Seed / step shape as the client platform's Random. This module is selm stdlib for target = "erlang". Browser apps use that Random. generate returns a Task (not a browser Cmd).

Types

Generator

type Generator a = Generator

A recipe for producing a random a.

Examples

int 1 6 : Generator Int

Seed

type Seed = Seed

Reproducible RNG state for step.

Examples

initialSeed 42 : Seed

Values

int

int : Int -> Int -> Generator Int

Generate integers in an inclusive range.

Examples

die : Generator Int
die =
    int 1 6

float

float : Float -> Float -> Generator Float

Generate floats in a range.

Examples

probability : Generator Float
probability =
    float 0 1

uniform

uniform : a -> List a -> Generator a

Pick uniformly from a non-empty set of values.

Examples

coin : Generator String
coin =
    uniform "heads" [ "tails" ]

weighted

weighted : (Float, a) -> List (Float, a) -> Generator a

Pick with weighted probability. Negative weights are treated as positive.

Examples

weighted (10, "common") [ (1, "rare") ]

constant

constant : a -> Generator a

Always produce the same value.

Examples

Tuple.first (step (constant 4) (initialSeed 0)) == 4

list

list : Int -> Generator a -> Generator (List a)

Generate a fixed-length list.

Examples

list 3 (int 1 6)
-- Generator (List Int) of length 3

pair

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

Generate a pair.

Examples

pair (int 0 10) (int 0 10)
-- Generator (Int, Int)

map

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

Transform a generated value.

Examples

map (\n -> n < 20) (int 1 100)
-- Generator Bool

map2

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

Combine two generators.

Examples

map2 Tuple.pair (int 0 9) (int 0 9)
-- Generator (Int, Int)

map3

map3 : (a -> b -> c -> d) -> Generator a -> Generator b -> Generator c -> Generator d

Combine three generators.

Examples

map3 (\a b c -> a + b + c) (int 1 6) (int 1 6) (int 1 6)
-- Generator Int

map4

map4 : (a -> b -> c -> d -> e) -> Generator a -> Generator b -> Generator c -> Generator d -> Generator e

Combine four generators.

Examples

map4 (\a b c d -> [a, b, c, d])
    (int 1 6) (int 1 6) (int 1 6) (int 1 6)

map5

map5 : (a -> b -> c -> d -> e -> f) -> Generator a -> Generator b -> Generator c -> Generator d -> Generator e -> Generator f

Combine five generators.

Examples

map5 (\a b c d e -> a + b + c + d + e)
    (int 1 6) (int 1 6) (int 1 6) (int 1 6) (int 1 6)

andThen

andThen : (a -> Generator b) -> Generator a -> Generator b

Chain generators.

Examples

int 1 3
    |> andThen (\n -> list n (int 0 9))
-- Generator (List Int) of length 1..3

lazy

lazy : (() -> Generator a) -> Generator a

Delay building a generator (for recursive generators).

Examples

lazy (\_ -> constant 1)

maxInt

maxInt : Int

Largest Int with good randomness guarantees (2147483647).

Examples

maxInt == 2147483647

minInt

minInt : Int

Smallest Int with good randomness guarantees (-2147483648).

Examples

minInt == -2147483648

generate

generate : Generator a -> Task x a

Run a generator once using OTP entropy. Prefer Task.perform / attempt.

Examples

Task.perform GotFace (generate (int 1 6))

step

step : Generator a -> Seed -> (a, Seed)

Step a generator with an explicit seed (deterministic).

Examples

Tuple.first (step (int 1 6) (initialSeed 42))
-- same value every time for seed 42

initialSeed

initialSeed : Int -> Seed

Build a seed from an integer (same input, same sequence).

Examples

initialSeed 42

independentSeed

independentSeed : Generator Seed

A generator of independent seeds.

Examples

generate independentSeed
-- Task x Seed