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 = GeneratorA recipe for producing a random a.
Examples
int 1 6 : Generator IntSeed
type Seed = SeedReproducible RNG state for step.
Examples
initialSeed 42 : SeedValues
int
int : Int -> Int -> Generator IntGenerate integers in an inclusive range.
Examples
die : Generator Int
die =
int 1 6float
float : Float -> Float -> Generator FloatGenerate floats in a range.
Examples
probability : Generator Float
probability =
float 0 1uniform
uniform : a -> List a -> Generator aPick uniformly from a non-empty set of values.
Examples
coin : Generator String
coin =
uniform "heads" [ "tails" ]weighted
weighted : (Float, a) -> List (Float, a) -> Generator aPick with weighted probability. Negative weights are treated as positive.
Examples
weighted (10, "common") [ (1, "rare") ]constant
constant : a -> Generator aAlways produce the same value.
Examples
Tuple.first (step (constant 4) (initialSeed 0)) == 4list
list : Int -> Generator a -> Generator (List a)Generate a fixed-length list.
Examples
list 3 (int 1 6)
-- Generator (List Int) of length 3pair
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 bTransform a generated value.
Examples
map (\n -> n < 20) (int 1 100)
-- Generator Boolmap2
map2 : (a -> b -> c) -> Generator a -> Generator b -> Generator cCombine 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 dCombine three generators.
Examples
map3 (\a b c -> a + b + c) (int 1 6) (int 1 6) (int 1 6)
-- Generator Intmap4
map4 : (a -> b -> c -> d -> e) -> Generator a -> Generator b -> Generator c -> Generator d -> Generator eCombine 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 fCombine 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 bChain generators.
Examples
int 1 3
|> andThen (\n -> list n (int 0 9))
-- Generator (List Int) of length 1..3lazy
lazy : (() -> Generator a) -> Generator aDelay building a generator (for recursive generators).
Examples
lazy (\_ -> constant 1)maxInt
maxInt : IntLargest Int with good randomness guarantees (2147483647).
Examples
maxInt == 2147483647minInt
minInt : IntSmallest Int with good randomness guarantees (-2147483648).
Examples
minInt == -2147483648generate
generate : Generator a -> Task x aRun 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 42initialSeed
initialSeed : Int -> SeedBuild a seed from an integer (same input, same sequence).
Examples
initialSeed 42independentSeed
independentSeed : Generator SeedA generator of independent seeds.
Examples
generate independentSeed
-- Task x Seed