module use "std/random.nib"
Random
Random integers: Random.Source.next_int(bound) draws one from 0 up to bound. Random.System is the real source; a reproducible one is a few lines of your own.
Random has one interface, Random.Source, with one operation: next_int(bound) answers an integer drawn uniformly from 0 up to, but not including, bound. Every other range is built from that:
use "std/random.nib"
module Dice where
func roll :: -> Int ! Random.Source raises Err.OutOfRange =
1 + @Random.Source.next_int(6)
func coin :: -> String ! Random.Source raises Err.OutOfRange =
if @Random.Source.next_int(2) == 0 then "heads" else "tails"
func main :: -> String ! Random.Source raises Err.OutOfRange =
"rolled {Dice.roll()}, flipped {Dice.coin()}"
wire W runs Dice.main where Random.Source = Random.System
Bind Random.System for real randomness. A function that draws says ! Random.Source, so its signature tells a reader its answer is not fixed by its arguments.
A reproducible source
There is no seed setting on Random.System. When a run must be repeatable — a test, a simulation you want to replay — write a seeded instance in the program, so the seed is somewhere a reader can see it:
use "std/random.nib"
effector Seeded is Random.Source where
config seed :: Int = 1;
unkeyed var state :: Int = 0;
next_int(bound) =
if bound <= 0 then raise Err.OutOfRange("no integer below {bound}")
else (state := (state * 75 + seed + 74) % 65537;
try state % bound rescue Err.DivisionByZero(_) => 0 end)
module Dice where
func main :: -> Vec<Int> ! Random.Source raises Err.OutOfRange =
let a = @Random.Source.next_int(6);
let b = @Random.Source.next_int(6);
let c = @Random.Source.next_int(6);
#[a, b, c]
wire Test runs Dice.main where Random.Source = Seeded { seed: 42 }
#[2, 2, 0]
Run it twice and the answer is the same.
Interfaces
interface Source where
next_int(bound: Int) -> Int raises OutOfRange
One draw. next_int(bound) answers an integer n with 0 <= n < bound, every value equally likely: next_int(10) answers 0 to 9, and next_int(1) always answers 0. Raises Err.OutOfRange when bound is zero or negative, since there is no integer to choose.
use "std/random.nib"
module Pick where
func main :: -> String ! Random.Source =
try "{@Random.Source.next_int(0)}"
rescue Err.OutOfRange(msg) => "refused: {msg}" end
wire W runs Pick.main where Random.Source = Random.System
refused: Random.Source.next_int: bound must be positive, got 0
Effectors
effector Random.System is Random.Source -- installed by nibc
next_int :: Int -> Int raises Err.OutOfRange
The operating system's randomness, installed by nibc. Every run draws different numbers, and there is no way to seed it; write an instance of your own when a run must be repeatable.
effector Random.Sim is Random.Source -- installed by nibc
next_int :: Int -> Int raises Err.OutOfRange
Draws from the simulated world's seed (--world FILE), for testing. The same seed draws the same numbers on every run, and each draw is written to the journal as a choice, so a run that randomises — a backoff, an election timeout — replays and can be steered. Bind it beside Net.Sim and Time.Sim in a test's wire and keep Random.System in production.
use "std/random.nib"
module Dice where
func main :: -> Int ! Random.Source raises Err.OutOfRange =
1 + @Random.Source.next_int(6)
wire W runs Dice.main where Random.Source = Random.Sim