Skip to main content

randomInRange

The randomInRange() function Generates a random number within a given range.

Parameters

ParameterTypeDescription
minNumberThe minimum value of the range.
maxNumberThe maximum value of the range.

Returns

  • Number: A random number within the given range

Throws

  • Error: if one of or both the arguments aren't a valid numbers
  • Error: if the maximum value is less than the minimum value

Example

const utils = require('utils-core.js');
const randomInt = randomInRange(1, 10);
console.log(randomInt);
// Output: a random integer between 1 and 10, for example: 4
const utils = require('utils-core.js');
const randomInt = randomInRange(1900, 2023);
console.log(randomInt);
// Output: a random integer between 1900 and 2023, for example: 1945

See also