Skip to content

Random

Draw random numbers, picks and strings.

Random values follow the run’s seed, so a run can be repeated; see random values and seeds.

randbool

impure
randbool(probability...) → bool

Return true with the given probability (default 0.5).

ParameterTypeDescription
probability...numberChance of true, from 0 to 1. Optional.
randbool(0.1)

→ false

randchoice

impure
randchoice(list) → any

Return one element of a list, picked at random.

ParameterTypeDescription
listlist
randchoice(["eu", "us", "apac"])

→ "us"

randfloat

impure
randfloat(min, max) → number

Return a random number at least min and below max.

ParameterTypeDescription
minnumber
maxnumber
floor(randfloat(5, 500) * 100) / 100

→ 123.45

randhex

impure
randhex(length) → string

Return a random lowercase hex string of the given length.

ParameterTypeDescription
lengthnumber
randhex(12)

→ "9f04c1a77be2"

randint

impure
randint(min, max) → number

Return a random integer between min and max, inclusive.

ParameterTypeDescription
minnumberSmallest value, an integer.
maxnumberLargest value, an integer.
randint(1, 6)

→ 4

randnormal

impure
randnormal(mean, stddev) → number

Return a random number from a normal distribution.

Most values land within one stddev of mean; use max/min to clamp the tails.

ParameterTypeDescription
meannumber
stddevnumber
round(randnormal(40, 10))

randsample

impure
randsample(list, n) → list

Return n distinct elements of a list, in random order.

ParameterTypeDescription
listlist
nnumberHow many elements, at most the list’s length.
randsample(["a", "b", "c", "d"], 2)

→ ["c", "a"]

randshuffle

impure
randshuffle(list) → list

Return a list’s elements in random order.

ParameterTypeDescription
listlist
randshuffle([1, 2, 3])

→ [3, 1, 2]

randstring

impure
randstring(length, charset...) → string

Return a random string of the given length.

Characters come from charset, letters and digits by default.

ParameterTypeDescription
lengthnumber
charset...stringCharacters to draw from. Optional.
randstring(8)

→ "q3ZxT0bA"

randstring(6, "0123456789")

→ "402913"

randweighted

impure
randweighted(weights) → string

Return a key of a map, picked with probability proportional to its weight.

ParameterTypeDescription
weightsmap(number)Keys to choose from, each with a non-negative weight.
randweighted({ card = 70, paypal = 20, bank = 10 })

→ "card"