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);
生成不重復隨機數有沒有性能更好的方法?
梵蒂岡之花
2018-10-16 17:18:13