码迷,mamicode.com
首页 > Web开发 > 详细

js 随机数

时间:2017-10-10 14:29:16      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:isp   one   doc   opened   har   view   分享   ==   UI   

技术分享
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title></title>
 6     <meta charset="utf-8" />
 7     <script>
 8         //取值范围min-max;
 9         //能随机到min和max[min,max]
10         function rnd(min, max) {
11             var tmp = min;
12             if (max < min) { min = max; max = tmp; }//交换值
13             return Math.floor(Math.random() * (max - min + 1) + min); //向下取整
14         }
15         for (i = 0; i < 10; i++) {
16             // document.write(rnd(5, 7)+"<br/>");
17         }
18 
19     </script>
20     <script>
21         //取值范围min-max;
22         //能随机到min 不能随机到max   [min,max)
23         function rnd(min, max) {
24             var tmp = min;
25             if (max < min) { min = max; max = tmp; }//交换值
26             return Math.floor(Math.random() * (max - min) + min); //向下取整
27         }
28         for (i = 0; i < 10; i++) {
29             // document.write(rnd(1,5) + "<br/>");
30         }
31     </script>
32     <script>
33         //取值范围min-max;
34         //能随机到max 不能随机到min   (min,max]
35         function rnd(min, max) {
36             var tmp = min;
37             if (max < min) { min = max; max = tmp; }//交换值
38             return Math.ceil(Math.random() * (max - min) + min); //向上取整
39         }
40         for (i = 0; i < 10; i++) {
41            // document.write(rnd(1, 5) + "<br/>");
42         }
43     </script>
44 
45     <script>
46         //取值范围min-max;
47         //不能随机到min和max(min,max)
48         function rnd(min, max) {
49             var tmp = min;
50             var temp;
51             if (max < min) { min = max; max = tmp; }//交换值
52             temp = Math.floor(Math.random() * (max - min + 1) + min); //向下取整
53             if (min == temp) {
54                 return min + 1;
55             } else { return temp; }
56         }
57     </script> 
58     <!-- 
59         var a = max/min;              a = 7;
60         var xs = (a - 1);             xs = 7/1 - 1 = 6; 
61         var x=(max-1.0001)/min-1      x ((7-1.0001)/1)-1 = 5.9995;     x/xs = 5.9995/6 = 0.99991
62         (7 - 1) * (0.999 *(x/xs)))  = 6.9999   向上取整   ==6
63          1 + 6 * (0.1 *(x/xs))  = 1.09991   向上取整      ==2
64         原理是 最小数 + 最小数{(a-1)(随机数)(缩小比例x/xs)}
65         1(1+6)(0.999*0.9999) =       
66                 当min=1时候; a= max;
67         -->
68     <script>
69         function rnd(min,max){
70             var a = max / min;
71             var xs = (a - 1);
72             var x = (max - 1.0001) / min - 1;
73             return Math.ceil(min * (1 + (a - 1) * (Math.random() *(x/xs))));
74         }
75         for (i = 0; i < 100; i++) {
76             document.write("xxx开区间" + rnd(1,7) + "<br/>");
77         }
78 
79     </script>
80 </head>
81 <body>
82 
83 </body>
84 </html>
View Code

 

js 随机数

标签:isp   one   doc   opened   har   view   分享   ==   UI   

原文地址:http://www.cnblogs.com/enych/p/7644924.html

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