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