Skip to main content
randomInt - node__crypto.d.ts - Node documentation
function randomInt

Usage in Deno

```typescript import { randomInt } from "node:node__crypto.d.ts"; ```
randomInt(max: number): number
Return a random integer `n` such that `min <= n < max`. This implementation avoids [modulo bias](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias). The range (`max - min`) must be less than 2**48. `min` and `max` must be [safe integers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger). If the `callback` function is not provided, the random integer is generated synchronously. ```js // Asynchronous const { randomInt, } = await import('node:crypto'); randomInt(3, (err, n) => { if (err) throw err; console.log(`Random number chosen from (0, 1, 2): ${n}`); }); ``` ```js // Synchronous const { randomInt, } = await import('node:crypto'); const n = randomInt(3); console.log(`Random number chosen from (0, 1, 2): ${n}`); ``` ```js // With `min` argument const { randomInt, } = await import('node:crypto'); const n = randomInt(1, 7); console.log(`The dice rolled: ${n}`); ```

Parameters

max: number
End of random range (exclusive).

Return Type

number
randomInt(
min: number,
max: number,
): number

Parameters

min: number
max: number

Return Type

number
randomInt(
max: number,
callback: (
err: Error | null,
value: number,
) => void
,
): void

Parameters

max: number
callback: (
err: Error | null,
value: number,
) => void

Return Type

void
randomInt(
min: number,
max: number,
callback: (
err: Error | null,
value: number,
) => void
,
): void

Parameters

min: number
max: number
callback: (
err: Error | null,
value: number,
) => void

Return Type

void