0

I'm working on a project for school. I need to generate an array of 15 random integers between 1 & 50. I have a function, but I would not like for the numbers to repeat. (for example, if the number 3 is located at index 0, I would not like for it to show up again in the array.) If I could get some help on not getting repeat numbers, that would be great.

Thank you for any help!

var arr;

function genArray() {
    //generates random array
    arr = [];
    for (var i = 0; i < 15; i++) {
        var min = 1;
        var max = 50;
        var arrayValue = Math.floor(Math.random() * (max - min + 1)) + min;
        arr.push(arrayValue);
    }
    arr.sort(function(a, b) {
        return a - b
    });
    console.log(arr);
}
kiyah
  • 1,502
  • 2
  • 18
  • 27

5 Answers5

0

In the loop generate a new random number while the number is in the array. In other words only continue when the new number is not in the array already.

var arr;

function genArray() {
  //generates random array
  arr = [];
  for (var i = 0; i < 15; i++) {
    var min = 1;
    var max = 50;
    do
    {
      var arrayValue = Math.floor(Math.random() * (max - min + 1)) + min;
    }while(arr.includes(arrayValue))
    arr.push(arrayValue);
  }
  arr.sort(function(a, b) {
    return a - b
  });
  console.log(arr);
}

genArray();
mdatsev
  • 3,054
  • 13
  • 28
0

You can make a function in which check the number if its already in array than regenrate the number else push the number in array

var arr;

    function genArray() {
      //generates random array
      arr = [];
      for (var i = 0; i < 15; i++) {
        var min = 1;
        var max = 50;
        var arrayValue = Math.floor(Math.random() * max) + min;
        if(checkno(arrayValue)==true)
           arr.push(arrayValue);
      } 
      arr.sort(function(a, b) {
        return a - b
      });
      console.log(arr);
    }

    function checkno(var no)
    {
    for(var i=0;i<arr.length;i++)
        {
           if(arr[i]==no)
              return false;
           else
              return true;
        }
    }
0

An alternate solution involves the Set object, sets only have unique elements, multiple elements of the same value are ignored.

Example of the set object implemented for this use:

var temp = new Set();
while (temp.size < 15) {
  var min = 1;
  var max = 50;
  temp.add(Math.floor(Math.random()*(max-min+1))+min);
}
Phil
  • 311
  • 1
  • 6
0

This approach uses Arrow functions, forEach and includes functions.

let LENGTH = 15;
let numbers = new Array(LENGTH).fill();

let findRandomNumber = (i) => {
  let rn;
  while (numbers.includes((rn = Math.floor(Math.random() * 50) + 1))) {}
  numbers[i] = rn;
};

numbers.forEach((_, i) => findRandomNumber(i));
console.log(numbers.sort((a, b) => a - b));
.as-console-wrapper {
  max-height: 100% !important
}
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You do not need to check the resulting array and regenerate the number. It is not efficient.
Please take a look at the following snippet:

function get_N_rand(N = 15, min = 1, max = 50) { // set default values
  var N_rand = [], range = [];
  for (var i = min; i <= max;) range.push(i++); // make array [min..max]
  while (N_rand.length < N) { // cut element from [min..max] and put it into result
    var rand_idx = ~~(Math.random() * range.length);
    N_rand.push(range.splice(rand_idx, 1)[0]);
  }
  return N_rand;
}

console.log(JSON.stringify( get_N_rand() )); // run with defaults
console.log(JSON.stringify( get_N_rand(6, 10, 80) )); // run with arbitraries
Kosh
  • 16,966
  • 2
  • 19
  • 34