APIRandom

Random

Random numbers as an effect.

Code that needs randomness performs Random; the program chooses where the numbers come from by running it under runSeeded, for reproducible draws, or runSystem, for fresh ones.

Random

effect RandomSource

Drawing random integers. A main that performs it runs under runSystem unless it installs another runner.

Operations

int

int : Int -> Int ->{Random} IntSource

A draw from lo to hi inclusive; reversed bounds are swapped.

runSeeded

runSeeded : Int -> (() ->{Random, e} a) ->{e} aSource

Answers every draw from a deterministic generator started at seed, so the same seed produces the same draws, in the interpreter and in compiled programs alike. The generator is a 31-bit linear congruential one with glibc’s constants: it is modulo-biased and not suitable for cryptography. Nested runs keep separate sequences.

Tasks that inherit the runner share its sequence. Each draw consumes one step exactly once, so concurrent tasks never repeat a draw, but which task receives which draw depends on their scheduling.

import Random

roll = Random.runSeeded 7 { Random.int 1 6 }

roll >= 1 && roll <= 6
Random.runSeeded 7 { Random.int 1 6 } == roll

runSystem

runSystem : (() ->{Random, e} a) ->{IO, e} aSource

runSeeded from a seed drawn from system entropy, so every run draws a fresh sequence.