JSFiddle - React, Tailwind, and code Playground
JavaScript
function randomNum(max, used, calls){
if( calls == void 0 ) calls = 0;
if( calls++ > 10000 ) return undefined;
if( used.length >= max ) return undefined;
var newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
return newNum;
}else{
return randomNum(max,used,calls);
}
}
var usedNums = [1,2,3,4,5,6,7,8,9];
var randNum = randomNum((10), usedNums);
usedNums.push(randNum);
console.log(usedNums);