Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Random

Source: lib/random.x

Guide: Time, Random Values, and Encodings

Pseudorandom numbers, system entropy, and UUIDs.

Include it with #include "random.x".

Primary API

FunctionSummary
Random.belowReturns an integer drawn uniformly from 0 through bound - 1.
Random.choiceReturns an element of items chosen uniformly.
Random.doubleReturns a double drawn uniformly from [0, 1), with 53 random bits.
Random.from_entropyReturns a generator seeded from Random.secure_fill, so each call starts a different sequence.
Random.newReturns a generator whose sequence seed fixes: equal seeds yield equal sequences.
Random.nextReturns the next 64 random bits and advances r.
Random.secure_fillFills count bytes at bytes from the operating system’s cryptographic generator, suitable for keys, tokens, and nonces.
Random.shufflePuts the elements of items in a uniformly random order, in place, with the Fisher-Yates shuffle.
Uuid.is_validReports whether text is a UUID that Uuid.parse accepts.
Uuid.parseReturns the UUID text spells in its canonical lowercase form.
Uuid.v4Returns a new random version 4 UUID in its canonical form: 36 lowercase characters such as 9f6e2c1a-0b7d-4e3f-a2c5-6d8e9f0a1b2c.

Random

Random.below

long Random.below(Random &r, long bound)

Returns an integer drawn uniformly from 0 through bound - 1. Draws that would favor small results are rejected, so every result is equally likely.

Raises: <bad-arg> when bound is not positive.

Source: lib/random.x

Random.choice

Var Random.choice(Random &r, Array items)

Returns an element of items chosen uniformly.

Raises: <bad-arg> when items is empty.

Source: lib/random.x

Random.double

double Random.double(Random &r)

Returns a double drawn uniformly from [0, 1), with 53 random bits.

Source: lib/random.x

Random.from_entropy

Random Random.from_entropy(void)

Returns a generator seeded from Random.secure_fill, so each call starts a different sequence.

Raises: <io-fail> when the system generator fails.

Source: lib/random.x

Random.new

Random Random.new(uint64_t seed)

Returns a generator whose sequence seed fixes: equal seeds yield equal sequences. SplitMix64 expands the seed into the state.

Source: lib/random.x

Random.next

uint64_t Random.next(Random &r)

Returns the next 64 random bits and advances r.

Source: lib/random.x

Random.secure_fill

void Random.secure_fill(void *bytes, size_t count)

Fills count bytes at bytes from the operating system’s cryptographic generator, suitable for keys, tokens, and nonces.

Raises: <io-fail> with errno when the system generator fails.

Source: lib/random.x

Random.shuffle

void Random.shuffle(Random &r, Array items)

Puts the elements of items in a uniformly random order, in place, with the Fisher-Yates shuffle.

Source: lib/random.x

Uuid

Uuid.is_valid

int Uuid.is_valid(String text)

Reports whether text is a UUID that Uuid.parse accepts.

Source: lib/random.x

Uuid.parse

String Uuid.parse(String text)

Returns the UUID text spells in its canonical lowercase form. text must be 32 hexadecimal digits of either case, grouped 8-4-4-4-12 by hyphens; any version and variant is accepted.

Raises: <malformed> with reason and byte offset when it is not.

Source: lib/random.x

Uuid.v4

String Uuid.v4(void)

Returns a new random version 4 UUID in its canonical form: 36 lowercase characters such as 9f6e2c1a-0b7d-4e3f-a2c5-6d8e9f0a1b2c. Its 122 random bits come from Random.secure_fill.

Raises: <io-fail> when the system generator fails.

Source: lib/random.x

Public types

TypeKindSummary
RandomstructA xoshiro256** pseudorandom generator, from Blackman and Vigna, “Scrambled Linear Pseudorandom Number Generators” (2018).
UuidaliasThe receiverless owner of Uuid.v4 and the other UUID operations.

Random

typedef struct Random { uint64_t state[4]; } Random

A xoshiro256** pseudorandom generator, from Blackman and Vigna, “Scrambled Linear Pseudorandom Number Generators” (2018). The value holds the whole generator state, so a copy continues the same sequence independently. Construct one with Random.new or Random.from_entropy.

Source: lib/random.x

Uuid

typedef Symbol Uuid

The receiverless owner of Uuid.v4 and the other UUID operations.

Source: lib/random.x

Design notes

Random is a seedable generator whose sequence its seed fixes; it is fast and unsuitable for secrets. Random.secure_fill reads the operating system’s cryptographic generator, arc4random_buf on macOS, getentropy on WebAssembly, and getrandom on Linux. Version 4 UUIDs take their bits from it.