Get Random Number
Generates a random number within a specified range, with flexible options for including or excluding boundaries.
getRandomNumber
The getRandomNumber function generates a random number within a specified range, with flexible options for including or excluding the boundary values. It defaults to generating a number between 0 and 100.
Function Signature
function getRandomNumber(options?: RandomNumberOptions): numberParameters
options(RandomNumberOptions, optional): Configuration object for the random number generation.min(number, optional): The minimum value of the range (inclusive). Defaults to0.max(number, optional): The maximum value of the range (inclusive). Defaults to100.includeMin(boolean, optional): Whether to include the minimum value. Defaults totrue.includeMax(boolean, optional): Whether to include the maximum value. Defaults totrue.
Return Value
number:- Returns a randomly generated number based on the provided options.
- The number will be within the specified range, with boundaries determined by the
includeMinandincludeMaxflags.
Example Usage
playground.ts
Notes
- If
minis greater thanmax, the function automatically swaps the values before generating the random number. - Supports multiple combinations of inclusive and exclusive boundaries to give complete control over the generated range.
- If both
minandmaxare equal, the function will simply return theminvalue as the random number.
Types
RandomNumberOptions
interface RandomNumberOptions {
min?: number;
max?: number;
includeMin?: boolean;
includeMax?: boolean;
}Aliases
The following aliases can be used for the getRandomNumber function:
getRandomInt
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
