Javascript获取任意随机整数的方法

<script type="text/javascript">
$(document).ready(function() {
var num=getRandom(5,20); //5-20之间的随机数,包括5和20
alert(num);
});
//获取随机数      
function getRandom(x,y){   
    //x 下限 
       //y 上限

   return parseInt(Math.random() * (y - x + 1) + x) ;

}  

        parseInt(10*Math.random())          //输出0~10之间的随机整数  

        Math.floor(Math.random()*10+1) //输出1~10之间的随机整数

</script>

猜你喜欢

转载自blog.csdn.net/qq15577969/article/details/80285245