@std/random

API Reference

class CryptoRng

Non-deterministic OS entropy for keys and tokens. Never seed-dependent.

fn int(): Int

One cryptographic random word from the OS.

returns — unsigned 64-bit value as Int.

class Rng

Seeded Xoshiro256++ stream. Same seed replays the same sequence, which makes randomized tests reproducible.

init(seed: Int)

fields

  • state: Array<Int>
fn bool(): Bool

Fair coin flip.

returns — true on even draws, false on odd draws.

fn int(min: Int, max: Int): Int

Draw an integer in [min, max], inclusive on both ends.

min — lower bound.

max — upper bound.

returns — deterministic draw from the stream.

import { Rng } from "@std/random";

let r = Rng.seeded(7);
print(r.int(1, 10));
Run in Playground
fn next(): Int

Next raw word of the stream, advancing internal state.

returns — signed 64-bit draw.

fn seeded(seed: Int): Rng

Construct a stream from a seed.

seed — any integer; equal seeds give equal streams.

returns — fresh Rng.

import { Rng } from "@std/random";

let a = Rng.seeded(7);
let b = Rng.seeded(7);
print(a.next() == b.next());
Run in Playground