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
| Name | Type | Description |
|---|---|---|
| min | number | Inclusive safe integer lower bound. |
| max | number | Exclusive safe integer upper bound. |
| callback | Function | Optional 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));