如何生成1-10个唯一数字,我这样做了
function getRandomArbitrary(min, max) {
var number = Math.floor(Math.random() * (max - min) + min);
return number;
}
但有时会返回重复的数字 我希望所有的号码在他们再次出现前再次出现
答案 0 :(得分:1)
尝试以下方法:
function shuffle(arra1) {
var ctr = arra1.length, temp, index;
while (ctr > 0) {
index = Math.floor(Math.random() * ctr);
ctr--;
temp = arra1[ctr];
arra1[ctr] = arra1[index];
arra1[index] = temp;
}
return arra1;
}
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9,10];
console.log(shuffle(myArray));