Random integer in range

OSCC · JavaScript, Math, Random · Oct 22, 2020

Generates a random integer in the specified range.

  • Use Math.random() to generate a random number and map it to the desired range.
  • Use Math.floor() to make it an integer.
const randomIntegerInRange = (min, max) =>
  Math.floor(Math.random() * (max - min + 1)) + min;
randomIntegerInRange(0, 5); // 2

Recommended snippets