random but not the same number in js code example
Example: math.random non repeat same number
let haveIt = [];
function generateUniqueRandom(maxNr) {
let random = (Math.random() * maxNr).toFixed();
random = Number(random);
if(!haveIt.includes(random)) {
haveIt.push(random);
return random;
} else {
if(haveIt.length < maxNr) {
return generateUniqueRandom(maxNr);
} else {
console.log('No more numbers available.')
return false;
}
}
}
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log(generateUniqueRandom(10));
console.log('Unique random numbers:' ,haveIt);