赞
踩
1、Math.random()返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。
Math.random() //0.2527967654612433
2、任意范围内的随机数:[min,max)
function getRandom(min,max){
return Math.random()*(max-min)+min
}
getRandom(1,2); //1.2038599854188117
3、任意范围的随机整数:[min,max)
//法一:
function getRnadomInt(min,max){
return Math.floor(Math.random()*(max-min))+min
}
console.log(getRnadomInt(1,6)); //4
//法二:
function getRnadomInt(min,max){
return parseInt(Math.random()*(max-min))+min
}
4、生成随机整数包含临界值:[min,max]
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。