一。生成一组手机号码
getMoble(){
var numArray = new Array("139","138","137","136","135","134","159","158","157","150","151","152","188","187","182","183","184","178","130","131","132","156","155","186","185","176","133","153","189","180","181","177"); //这是目前找到的除了数据卡外的手机卡前三位,类型是字符串数组
var numList = ""; //创建一个数组用来存放10个手机号
var arraryLength = numArray.length; //获取数组长度,这样如果手机号前三位取值单位发生变化,在下一步求i的地方就不用修改随机数取值范围了
for( var n = 0; n < 10; n++){
var i = parseInt( Math.random() * arraryLength); //注意乘以的是上面numArray数组的长度,这样就可以取出数组中的随机一个数。random的取值范围是大于等于0.0,小于1.0,相乘后得到的就是0到(数组长度-1)的值。
var num = numArray[i]; //取出随机的手机号前三位并赋值给num,手机号前三位是字符串类型的
for ( var j = 0; j < 8; j++){
num = num + Math.floor(Math.random() * 10); //num是字符串,后面的数字被当做字符串。所以变成两个字符串拼接了
var result = num.substr(0,3) + "****" + num.substr(7)
}
if(n == 0){
numList = numList + result; //第一个手机号前不出现“,”
}else{
numList = numList = numList +"获得了10元红包" + "," + result;//从第一个手机号后面到最后一个之前用逗号分隔
}
}
console.log(numList.split(',').slice(0,-1))
},
二。随机获取 x~y 之间的一个整数,其中 x 和 y 都是整数
Math.round(Math.random()*(y-x)+x)
例如:
- var x = 26,y = 37;
- alert(Math.round(Math.random()*(y-x)+x));