samedi 3 février 2018

I need to make an array of 15 random integers. I have a function but dont want numbers to repeat

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);
}




Aucun commentaire:

Enregistrer un commentaire