Skip to content

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 keywordsnumber, 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.

TypeOutputOptions
numberdecimal in [0, 1) by default; integers in [min, max] when both bounds are whole and decimals is not setmin, max, decimals
booltrue / falseprobability — chance of true, default 0.5
textrandom stringlength (default 10), chars (default A-Za-z0-9)
uuidUUID derived from the random stream
datedate in [min, max] (inclusive)min (default 1970-01-01), max (default 2070-01-01)
instantinstant 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 number

Returns 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 10

Other primitive types

import 'random'
from random uuid
from 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 pick
from random Color generator take 5 // a stream of picks