On this page
@std/random
Unstable
This @std package is experimental and its API may change without a major version bump.
Overview Jump to heading
Utilities for generating random numbers.
import { randomIntegerBetween } from "@std/random";
import { randomSeeded } from "@std/random";
import { assertEquals } from "@std/assert";
const prng = randomSeeded(1n);
assertEquals(randomIntegerBetween(1, 10, { prng }), 3);
Add to your project Jump to heading
deno add jsr:@std/random
See all symbols in @std/random on
Why use @std/random? Jump to heading
Random number generation is useful for simulations, games, sampling,and randomized algorithms. This package provides both convenience functions for common use cases and a way to create reproducible pseudo-random sequences via seeding.
Examples Jump to heading
import { randomIntegerBetween, randomSeeded } from "@std/random";
const prng = randomSeeded(123n);
const roll = randomIntegerBetween(1, 6, { prng });
Tips Jump to heading
- For reproducible tests and simulations, use
randomSeeded(seed)
and pass the PRNG to helpers. - For security-sensitive randomness (tokens, keys), use the Web Crypto API
(
crypto.getRandomValues
) instead of PRNG utilities. - Distribution helpers like
randomIntegerBetween
are inclusive of both bounds; document this in APIs to avoid off-by-one confusion.