生成彩票号码-javascript

时间:2018-12-10 17:51:36

标签: javascript frontend

我希望有人可以在这里帮助我。我试图从1-30十次生成一组6个唯一数字。现在,似乎有一个重复的数字滑入了数组的其中之一,我也希望能帮助您删除不确定的结果。

这不是重复项,因为我正在生成具有唯一编号的多个数组。我需要检查以下事实:我的唯一性检查不起作用,并且我得到重复的数字。

谢谢!

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

预先感谢您的帮助! 错误示例: https://imgur.com/a/GHGEhPS

代码: https://plnkr.co/edit/iQRM9YmKaSxqFiXpnu0X

0 个答案:

没有答案