Math产生的随机数,怎样使产生数字不相同
使用双层for循环,外部的for循环输出数组,内部的for循环判断输出的内容是否重复,如果重复,使外部for循环重新开始循环

var arr=[];
for (var i = 0; i < 6; i++) {
var randouNum= Math.floor(Math.random()*10)
var isHas=false;
for (var j = 0; j < arr.length; j++) {
if (arr[j]==randouNum) {
i--;
isHas=true;
break;
}
}
if (isHas==false) {
arr[arr.length]=randouNum
}
}
console.log(arr);

浙公网安备 33010602011771号