function rnd(min, max, count) {
var randoms = [];
while (true) {
var isExists = false;
// 获取一个min–max范围的数
var random = parseInt(min + (max - min) * (Math.random()))
// 判断当前随机数是否已经存在
for (var i = 0; i < randoms.length; i++) {
if (random === randoms[i]) {
isExists = true;
break;
}
}
// 如果不存在,则添加进去
if (!isExists)
randoms.push(random);
// 如果有10位随机数了,就跳出
if (randoms.length === count)
break;
}
return randoms;
}
console.log(rnd(0, 30, 8));
[19, 8, 1, 17, 16, 2, 27, 22] |