0

I am hoping someone can help me here. I am trying to generate a set of 6 unique numbers from 1 - 30 ten times. Right now, a duplicate number seems to slip into one of the array's, and I would also appreciate help with getting the undefined results removed.

This not a duplicate because I am generating multiple arrays with unique numbers. I need help with the fact that my check for uniqueness is not working and I'm getting duplicate numbers.

Thanks!

       var yourNum = {
              numbers : [5, 18, 3, 15, 2, 25, 8, 20, 16, 26, 27, 4, 9, 
                        28, 7, 11, 19, 10, 30, 23, 22, 12, 1, 6, 14, 21, 
                        13, 17,24, 29],
              results : [],
              numPicks : [],
              genRandomNum: function() {
                var numRandom;
          do{
        numRandom = Math.floor(Math.random() * (yourNum.numbers.length) 
          +1);
                //this is not checking for uniqueness every time 
                //   getting results such as [2,2,13,25,19,26]
          } while(yourNum.numPicks.indexOf(numRandom) !== -1);
                 return yourNum.numPicks.push(numRandom);   
     }
       };


        console.log();

        var genResults = function(num){

        var count = num;
         for(let i = 0; i < count; i++){
          for(let j = 0; j < 6; j++){
            if(yourNum.numPicks.length < 6) {

         yourNum.numPicks.push(yourNum.numbers[yourNum.genRandomNum()]);


        }      
      }

       yourNum.results[i] = yourNum.numPicks;
       yourNum.numPicks = [];


       }
         console.log("--------------NEW--------------");
         console.log(yourNum.results);

      }

        genResults(10);

Thank you in advance for help! example of the error: https://imgur.com/a/GHGEhPS

code: https://plnkr.co/edit/iQRM9YmKaSxqFiXpnu0X

Dan
  • 361
  • 1
  • 5
  • 17
  • 1
    I disagree that it's strictly a duplicate, but also the task is much easier than you're making it. You really just need to fill the array with unique numbers, not track all the things you're tracking> https://plnkr.co/edit/8P2ZdJFs63CrwuHpXXuV?p=catalogue – Paul Dec 10 '18 at 18:00
  • 1
    [I'd recommend simply using a `do..while` loop](https://jsbin.com/rozitetaqe/edit?js,console) – h2ooooooo Dec 10 '18 at 18:00
  • what about getting the loop to run 6 times to generate the array of unique numbers? – Dan Dec 10 '18 at 18:14
  • @bowl_of_rice Change `getResults(10)` to `getResults(6)`? – h2ooooooo Dec 11 '18 at 08:26

0 Answers0