For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/math/randomInt.md.

randomInt es-toolkit

Generates a random integer within a specified range.

const randomInteger = randomInt(min, max);

Usage

randomInt(maximum) / randomInt(minimum, maximum)

Use randomInt when you need a random integer. It returns only integers without decimal points.

import { randomInt } from 'es-toolkit/math';

// Generate a random integer between 0 and 5 (exclusive).
const num1 = randomInt(5);
console.log(num1); // e.g., 3

// Generate a random integer between 2 and 10 (exclusive).
const num2 = randomInt(2, 10);
console.log(num2); // e.g., 7

// Can be used with negative ranges.
const num3 = randomInt(-5, -1);
console.log(num3); // e.g., -3

// Simulate a dice roll (1-6)
const diceRoll = randomInt(1, 7);
console.log(diceRoll); // e.g., 4

// Select a random index from an array
const items = ['apple', 'banana', 'cherry', 'date'];
const randomIndex = randomInt(items.length);
console.log(items[randomIndex]); // e.g., 'banana'

Parameters

maximumnumberrequired

The maximum value (exclusive) when using a single parameter. Must be greater than 0.

minimumnumberrequired

The minimum value (inclusive).

maximumnumberrequired

The maximum value (exclusive). Must be greater than the minimum value.

Returns

Returns a random integer within the specified range.

number

Throws

Throws an error if the maximum value is less than or equal to the minimum value.

Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.