@std/random.ms#
Import each required symbol with import Name from "@std/random.ms".
SeededRandom Type#
A deterministic pseudo-random generator: the same seed always gives the same sequence, on every platform.
It is not secure; use secure_int, float, shuffle and choice when the values must be unpredictable.
After var rng = seeded(1), rng.int(0, 10) returns the same value on every run.
opaque type SeededRandom {
}SeededRandom.choice#
Returns a uniformly chosen item, or null for an empty list.
var fn choice[T](values List[T]) T?SeededRandom.exponential#
Returns an exponentially distributed Float with rate 1 (mean 1); never negative.
var fn exponential() FloatSeededRandom.float#
Returns a Float at least 0.0 and below 1.0 with 53 random bits.
var fn float() FloatSeededRandom.int#
Returns an Int at least min and below max_exclusive, uniformly. A min not
below max_exclusive, or a range wider than the largest Int, faults.
var fn int(min Int, max_exclusive Int) IntSeededRandom.next_bits#
Returns the next 64 random bits as a signed Int.
var fn next_bits() IntSeededRandom.normal#
Returns a standard normal Float (mean 0, standard deviation 1) using the Box-Muller transform.
var fn normal() FloatSeededRandom.permutation#
Returns a shuffled list of the Ints 0 through count - 1. A negative count
faults.
var fn permutation(count Int) List[Int]SeededRandom.sample#
Returns count items chosen uniformly without replacement, in random order, as
copies; values is unchanged. A count outside 0 through the list length
faults.
var fn sample[T Clonable](values List[T], count Int) List[T]SeededRandom.shuffle#
Shuffles values with an unbiased Fisher-Yates shuffle and returns it.
var fn shuffle[T](move values List[T]) List[T]SeededRandom.weighted_choice#
Returns an item chosen with probability proportional to its weight as a view, or
null for an empty list. weights must match values in length, hold no negative or
non-finite weight, and not all be zero; otherwise the result is null.
var fn weighted_choice[T](values List[T], weights List[Float]) T?choice Function#
Returns a uniformly chosen item as a view of values, or null for an empty list. Use
.clone() to keep it.
fn choice[T](values List[T]) Result[T?, Error] { ... }float Function#
Returns a secure Float at least 0.0 and below 1.0 with 53 random bits from the operating system’s entropy.
fn float() Result[Float, Error] { ... }random_bytes Function#
Returns exactly count unpredictable bytes from the operating system. count runs
from 0 through 16 MiB (16777216); a negative or larger count returns EINVAL and a
source failure returns EIO. The call observes task cancellation between 64 KiB
chunks.
fn random_bytes(count Int) Result[Bytes, Error] { ... }sample Function#
Returns count items chosen uniformly without replacement, in random order, as
copies; the input is unchanged. A count outside 0 through the list length returns
EINVAL.
fn sample[T Clonable](values List[T], count Int) Result[List[T], Error] { ... }secure_int Function#
Returns a secure integer from the operating system’s entropy, at least min and
below max_exclusive. When max_exclusive is null, the upper bound is the
largest Int. A min that is not below the bound returns EINVAL.
fn secure_int(min Int, max_exclusive Int? = null) Result[Int, Error] { ... }seeded Function#
Returns a generator that starts from seed. Equal seeds give equal sequences. The
algorithm is SplitMix64.
fn seeded(seed Int) SeededRandom { ... }shuffle Function#
Shuffles the list in place with an unbiased Fisher-Yates shuffle and returns it.
fn shuffle[T](move values List[T]) Result[List[T], Error] { ... }token Function#
Returns a random token of size bytes as unpadded URL-safe Base64.
The default 32 bytes (256 bits) is 43 characters. Use it for session ids, API keys and password-reset links.
size follows random_bytes: 0 through 16 MiB, otherwise EINVAL.
fn token(size Int = 32) Result[String, Error] { ... }ulid Function#
Creates a ULID: 26 uppercase Crockford Base32 characters holding the Unix time in milliseconds and 80 random bits.
A ULID made later sorts after an earlier one as a string; two made in the same millisecond sort in random order.
Example: "01ARZ3NDEKTSV4RRFFQ69G5FAV". An entropy failure returns an Error.
fn ulid() Result[String, Error] { ... }weighted_choice Function#
Returns a view of an item chosen with probability proportional to its weight, or null
for an empty list. weights must match values in length, hold no negative or
non-finite weight, and not all be zero; otherwise this returns EINVAL.
fn weighted_choice[T](values List[T], weights List[Float]) Result[T?, Error] { ... }