const indRandom = (n, maxValue, minValue = 0) => { const nNumArray = [...Array(maxValue + 1).keys()]; const resultArray = []; for (let i = 0; i < n; i++) { const randomNum = Math.floor((Math.random() * (((maxValue - minValue) + 1) - i)) + minValue); resultArray.push(nNumArray[randomNum]); nNumArray.splice(randomNum, 1); } console.log(resultArray); return resultArray;};indRandom(5, 10000);
1 回答
湖上湖
TA贡献2003条经验 获得超2个赞
function indRandom(n, max){
var arr = [];
for(var i = 0; i < n; i++){
var item = Math.floor(Math.random() * max);
if(arr.indexOf(item) > -1){ i--; continue; }
else arr.push(item);
}
return arr;
}
添加回答
举报
0/150
提交
取消
