-2

I'm working on a loop that adds 10 unique random integers from 1-10 to an array, so the expected result would be 2,1,4,6,10,7,9,8,5,3 I manage to generate random numbers and only add the unique ones, my problem is, its not complete 10 items, I think the problem is upon checking I it doesn't generate again.

Hope you help me.

Thanks.

for (let i = 0; i < 10; i++) {
  var arrNum = [];
  setTimeout(function() {
    var r = Math.floor(Math.random() * 10) + 1;
    while (arrNum.includes(r) == false) {
      arrNum.push(r);
    }
    if (i == 9) {
      $('ul').append('<li>' + arrNum + '</li>');
    }
  }, 500);
}
ul li{
  list-style-type: none;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>
user123
  • 435
  • 1
  • 6
  • 23
  • 2
    Possible duplicate of [Generate unique random numbers between 1 and 100](https://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100). See [this answer](https://stackoverflow.com/a/50652249/2019247) specifically. – 31piy Aug 20 '18 at 05:20
  • 1
    Or, one could ask: "How to shuffle an array?" (really, ask SO this question) – user2864740 Aug 20 '18 at 05:23
  • What happens if `r` is in the array? – Jonas Wilms Aug 20 '18 at 05:25
  • @user2864740 oh you mean [this?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array). too complicated right? – user123 Aug 20 '18 at 05:27

3 Answers3

2
  while (arrNum.includes(r) == false) {
   arrNum.push(r);
 }

That adds the number as long as it doesnt exist, so it will only run once, and if the number exists already it doesnt generate a new one, you want:

   const result = [];
   for(let i = 1; i <= 10; i++) {
       let random;
       do {
         random = 1 + Math.floor(Math.random() * 10);
      } while(result.includes(random));
      result.push(random);
  }

But its probably way more easy to just shuffle an array with the numbers 1 to 10.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • thanks this for sir, the approach is different that's why I choose this method. – user123 Aug 20 '18 at 05:38
  • @Mark While this does show valid logic and focuses on the original code/issue, a do-while is a *non-scaling* approach. For 10 items, it won't matter. – user2864740 Aug 20 '18 at 16:30
  • ... and to continue on @user2864740 it might even happen that this one runs forever ... (very unlikely though) – Jonas Wilms Aug 20 '18 at 17:40
1

Your code has a mistake. The i is not the ideal way to check for 10 occurrences as it is incremented even if variable r does not satisfy your condition - arrNum.includes(r) == false.

The check should be made by array length.

Replace if (i == 9) with if(arrNum.length = 10) in your code.

bitsobits
  • 104
  • 11
1

Create an array with numbers from 1 to N (first line). Replace N with 10 in your case. Then sort it randomly.

const numbers = [ ...Array(N).keys() ].map(o => o + 1);
numbers.sort(() => Math.random() - 0.5);
console.log(numbers); // [ 6, 4, 5, 2, 1, 9, 3, 7, 8, 10 ]
Felix Lemke
  • 6,189
  • 3
  • 40
  • 67