seededRandom

🎲 Creates a deterministic random-number generator from a seed.

Syntax

TypeScript
import { seededRandom } from '@opentf/std';
seededRandom(seed: number | string): () => number

Parameters

  • seed: The seed. Equal seeds replay equal streams.

Returns

A function producing the next float in [0, 1).

This is the opposite of randomInt and friends: same seed, same sequence, on every run and every runtime — for tests, games, and reproducible simulations. It composes with plain code: roll dice with Math.floor(rand() * 6) + 1, pick with arr[Math.floor(rand() * arr.length)].

A statistical generator, not a secure one. Anyone holding the seed replays the stream, so never use it for keys or tokens — that is what the cryptographic utils are for.

Throws a TypeError if the seed is neither a number nor a string.

Examples

TypeScript
const rand = seededRandom('demo');
rand() //=> 0.8388890530914068
rand() //=> 0.13535357755608857

Try it

Last updated on
Edit this page