[JavaScript] 亂數不重覆


function selRandom(min,max,sel){
//alert(min+'/'+max+'/'+sel);
var maxNum = max;
var minNum = min;
var selNum = sel-1;
var aArray=new Array;
var i=0,j=0,k=0;
//alert(aArray[selNum]);
while(aArray[selNum]==undefined){
var n = Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
flag
=0;
for (j=0;j<=selNum;j++){
if(n==aArray[j]){
flag=1;
break;
}
}
if(flag==0){
aArray[k]=n;
k=k+1;
}
}
var sReturn="";
for (j=0;j<=selNum;j++){

sReturn=sReturn+aArray[j]+",";

}
return sReturn;
}


沒有留言: