Random integer array in range

JavaScript, Math, Random · Oct 22, 2020

Generates an array of n random integers in the specified range.

  • Use Array.from() to create an empty array of the specific length.
  • Use Math.random() to generate random numbers and map them to the desired range, using Math.floor() to make them integers.
const randomIntArrayInRange = (min, max, n = 1) =>
  Array.from(
    { length: n },
    () => Math.floor(Math.random() * (max - min + 1)) + min
  );
randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ]

More like this

  • JavaScript Random Value Generators

    Quickly and easily generate random integers, strings, booleans, arrays and hex color codes using JavaScript.

    Collection · 7 snippets

  • Random integer in range

    Generates a random integer in the specified range.

    JavaScript, Math · Oct 22, 2020

  • Random number in range

    Generates a random number in the specified range.

    JavaScript, Math · Oct 22, 2020

  • Arithmetic progression

    Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

    JavaScript, Math · Oct 13, 2021