<script type="text/javascript">
var str="0123456789";
var arr=[];
var n;
while(arr.length<4){
n=Math.floor(Math.random()*str.length);
if (arr.length==0){
arr.push(n);
}else{
var un=true;
for(var i=0;i<arr.length;i++){
if(arr[i]==n){
un=false;
break;
}
};
if(un){
arr.push(n)
};
};
}
alert(arr)
</script>
转载于:https://www.cnblogs.com/busicu/p/4789065.html