Skip to content

Function randomBetween ​

Package: @litert/utils-numberSource: packages/partials/number/src/Functions/RandomBetween.ts

Generates a random integer within the half-open range [min, max), meaning min is inclusive and max is exclusive.


Import ​

ts
import { randomBetween } from '@litert/utils-number';

Signature ​

ts
function randomBetween(min: number, max: number): number;

Parameters ​

ParameterTypeDescription
minnumberInclusive lower bound
maxnumberExclusive upper bound

Return Value ​

A random integer n such that min <= n < max.


Error Handling ​

  • RangeError — thrown when min is greater than max.

Examples ​

ts
import { randomBetween } from '@litert/utils-number';

const n = randomBetween(0, 10); // 0, 1, 2, ..., 9

// When min equals max, the result is always min
console.log(randomBetween(5, 5)); // 5

// Throws RangeError
randomBetween(10, 0);