Files
30-seconds-of-code/snippets/random-integer-in-range.md
2017-12-13 09:47:16 +11:00

297 B

Random integer in range

Use Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

const randomIntegerInRange = (min, max) => 
  Math.floor(Math.random() * (max - min + 1)) + min;
// randomIntegerInRange(0, 5) -> 2