Crypto

crypto.randomInt()

Draws an integer in a half-open interval without modulo bias. The upper endpoint is excluded. Do not expect a repeatable random value in a fixture. Tested on Node.js 22.16.0. Example output (one line per entry): ["true"].

Formal Syntax & Structure

nodejs syntaxnodejs
crypto.randomInt([min,] max[, callback])

Parameters & Attributes

NameTypeDescription
minnumberInclusive safe integer lower bound.
maxnumberExclusive safe integer upper bound.
callbackFunctionOptional completion callback.

Return Value

number | undefined

Random integer or callback completion.

Working Example

Example Snippetnodejs
import { randomInt } from 'node:crypto';const picks = Array.from({ length: 16 }, () => randomInt(1, 7));console.log(picks.every(n => Number.isInteger(n) && n >= 1 && n < 7));
Node.js Reference