<script>
var res = [];
$(function () {
//生成0到200内60个不重复的随机数[0,200)
createRandom(60, 0, 200);
res = res.sort(sortNum);
for (var i = 0; i < res.length; i++) {
console.log(res[i]);
}
});
function createRandom(num, min, max) {
let arr = [];
for (let i = min; i < max; i++) {
arr.push(i);
}
DG(num, arr);
}
function DG(num, arr) {
let newArr= Object.assign([], arr);
for (let item = 0; item < arr.length; item++) {
res.push(newArr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
break;
}
if (res.length == num) {//满足随机数个数,ok
return res;
}
else {
DG(num, newArr);
}
}
function sortNum(a, b) {
return a - b;//升序,如降序,把“a - b”该成“b - a”
}
</script>