javascript求最大最小随机数

1. 第一种

1 random:function(min,max){
2        var min,max;
3        return Math.floor(Math.random()*max+min);
4 }

2.第二种

1 random:function(min,max){
2     //取值范围总数
3     var choices = max- min+ 1;
4     return Math.floor(Math.random() * choices + min);
5 }

 

两种方法都有效,个人感觉第二种更好用一点。

posted @ 2017-02-23 17:29  甘小春  Views(342)  Comments(0)    收藏  举报