码迷,mamicode.com
首页 > 编程语言 > 详细

产生num个不重复的随机数组

时间:2017-08-26 11:33:37      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:function   span   turn   产生   重复   rand   from   code   and   

 

 

    createDiffRandom : function (from,to,num) {    // 产生num个不重复的随机数组
            var arr=[],json={};      // 随机数数组 , 标记json对象                  
            while(arr.length<num) {               
                var ranNum=Math.floor(Math.random()*(to-from))+from;
                if(!json[ranNum]){         // 通过判断json对象的索引值是否存在 来标记 是否重复
                    json[ranNum]=1; arr.push(ranNum);                   
                }
            }
            return arr;
        },

 

产生num个不重复的随机数组

标签:function   span   turn   产生   重复   rand   from   code   and   

原文地址:http://www.cnblogs.com/web-fusheng/p/7434539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!