生成双色球解法
双色球分为红色和蓝色,红色有六位,在1~33之间随机产生,蓝色有一位,在1~16之间随机产生,如下图:
方法一:
var red=['1','2','3','4','5','6','7','8','9','10','11','12','13','14',
'15','16','17','18','19','20','21','22','23','24','25','26','27','28',
'29','30','31','32','33'];
var blue=['1','2','3','4','5','6','7','8','9','10','11','12','13','14',
'15','16'];
//随机取red6个
var temp=[];
while(temp.length<6){
var j=Math.floor(Math.random()*red.length);
//随机获取下标值
if(temp.indexOf(red[j])===-1){
//该判断条件保证数组数据唯一性
temp.push(red[j]);
//符合条件的放入数组
}
}
//从小到大排列
temp.sort(function(a,b){
return a-b;
})
//随机取blue1个
var ball=Math.floor(Math.random()*blue.length);
temp.push(ball);
console.log(temp);
方法二:
for(var i=1,red=[];i<34;i++){
red.push(i);
}
for(var i=1,blue=[];i<17;i++){
blue.push(i);
}
for(var i=0,temp=[];i<6;i++){
var index=Math.floor(Math.random()*red.length);
temp.push(red[index]);
red.splice(index,1);
}
temp.sort(function (a,b){
return a-b;
})
var index1=Math.floor(Math.random()*blue.length)+1;
temp.push(index1);
console.log(temp);