Skip to main content
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.

Example of generating a random integer with fixed seed number:

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); 

Example of generating a random integer between two values:

import { randomIntegerBetween } from "@std/random"; import { randomSeeded } from "@std/random"; const prng = randomSeeded(BigInt(crypto.getRandomValues(new Uint32Array(1))[0]!)); const randomInteger = randomIntegerBetween(1, 10, { prng }); 

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.

Did you find what you needed?