random
Module Export
random <type> [generator] [<options>]Generates a random value or an infinite generator of a given type.
The random macro generates a random value of the type that follows it. Without the generator keyword it produces a single value; with it, an infinite iterator of that type.
Type argument
The element after random selects the generation strategy and the static output type:
- Primitive keywords —
number,text,bool,uuid,date,instant(matched case-insensitively). - Named type symbols — any symbol that resolves to a type. Literal-union types produce a uniform pick among their members.
Unsupported inputs produce an analyzer error listing the supported types.
Supported types and options
All options are optional. Every type additionally accepts a common seed: text | number.
| Type | Output | Options |
|---|---|---|
number | decimal in [0, 1) by default; integers in [min, max] when both bounds are whole and decimals is not set | min, max, decimals |
bool | true / false | probability — chance of true, default 0.5 |
text | random string | length (default 10), chars (default A-Za-z0-9) |
uuid | UUID derived from the random stream | — |
date | date in [min, max] (inclusive) | min (default 1970-01-01), max (default 2070-01-01) |
instant | instant in [min, max) | min, max |
Options are type-checked against the chosen type — e.g. probability on uuid is an error.
The generator keyword
Placed after the type, generator turns the single value into an infinite iterator ([T]), meant to be composed with take, select, where, etc. The expression reads naturally: “a random number generator”.
Examples
A single value
import 'random'
from random numberReturns a decimal in [0, 1).
A die roll with bounds
import 'random'
from random number { min = 1, max = 6 }A deterministic stream
import 'random'import 'iterators'
from random number generator { seed = 42 } take 10Other primitive types
import 'random'
from random uuidfrom random bool { probability = 0.8 }from random date { min = 2024-01-01, max = 2024-12-31 }from random text { length = 8 }Picking from a union type
A named literal-union type produces a uniform pick among its members:
import 'random'
let Color: 'red' | 'green' | 'blue'
from random Color // one pickfrom random Color generator take 5 // a stream of picks