当前位置:   article > 正文

JavaScript随机数(random)_js random

js random

Math.random() 函数返回一个浮点, 伪随机数在范围[0,1),也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。

1、0~1的随机数?

Math.random();
  • 1

2、0~10的随机数?

Math.random()*10;
  • 1

3、0~10的随机整数?

 Math.floor(Math.random()*10);
  • 1

Math.floor() === 向下取整

4、100~1000的随机整数?

Math.floor(Math.random()*(1000-100)+100);
  • 1

5、生成10个不重复的随机整数?

 var nums=new Array();
            var p5=document.getElementById("p5");
            for(var i=0;;i++){
                // 10个随机数
                if(nums.length<10){
                    getRandom(10);
                }else{
                    break;
                }
            }
            //遍历数组
            for(var i=0;i<nums.length;i++){
                p5.innerHTML=nums;
            }    
            // 生成随机数
            function getRandom(count){
                var rand=Math.floor(Math.random()*count);
                for(var i=0;i<nums.length;i++){
                    if(nums[i]==rand){
                        return false;
                    }
                }
                nums.push(rand);
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

6、100~1000之间生成10个不重复的随机整数,并且降序排列?

100~1000之间的随机数,改变上面随机数的函数

var rand=Math.floor(Math.random()*(1000-100)+100);
  • 1

降序函数

nums.sort(function(a,b){return b-a;});
  • 1

7、0~5之间随机整数(包含5)?

Math.floor(Math.random()*6);
  • 1

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>随机数</title>
    <script>
        function click1(){
            var num=Math.random();
            var p1=document.getElementById("p1");
            p1.innerHTML=num;
        }
        function click2(){
            var num=Math.random()*10;
            var p1=document.getElementById("p2");
            p1.innerHTML=num;
        }
        function click3(){
            var p1=document.getElementById("p3");
            var p11=document.getElementById("p33");
            var num=Math.random()*10;
            p11.innerHTML=num;
            var num1=Math.floor(num);
            p1.innerHTML=num1;
        }
        function click4(){
            var p4=document.getElementById("p4");
            var num=Math.floor(Math.random()*(1000-100)+100);
            p4.innerHTML=num;
        }
        function click5(){
            var nums=new Array();
            var p5=document.getElementById("p5");
            for(var i=0;;i++){
                // 10个随机数
                if(nums.length<10){
                    getRandom(10);
                }else{
                    break;
                }
            }
            //遍历数组
            for(var i=0;i<nums.length;i++){
                p5.innerHTML=nums;
            }    
            // 生成随机数
            function getRandom(count){
                var rand=Math.floor(Math.random()*count);
                for(var i=0;i<nums.length;i++){
                    if(nums[i]==rand){
                        return false;
                    }
                }
                nums.push(rand);
            }
        }
        function click6(){
            var nums=new Array();
            var p6=document.getElementById("p6");
            for(var i=0;;i++){
                // 10个随机数
                if(nums.length<10){
                    getRandom(10);
                }else{
                    break;
                }
            }
            //遍历数组
            for(var i=0;i<nums.length;i++){
                nums.sort(function(a,b){return b-a;});//降序排列
                p6.innerHTML=nums;
            }    
            // 生成随机数
            function getRandom(count){
                var rand=Math.floor(Math.random()*(1000-100)+100);
                // var rand=Math.floor(Math.random()*count);
                for(var i=0;i<nums.length;i++){
                    if(nums[i]==rand){
                        return false;
                    }
                }
                nums.push(rand);
            }
        }
        function click7(){
            var num=Math.floor(Math.random()*6);
            var p7=document.getElementById("p7");
            p7.innerHTML=num;
        }
    </script>
</head>
<body>
    <ul>
        <li>
            <h1>1、0~1的随机数?</h1>
            <button onclick="click1()">点击查看</button>
            <p id="p1">
            </p>
        </li>
        <li>
            <h1>2、0~10的随机数?</h1>
            <button onclick="click2()">点击查看</button>
            <p id="p2"> 

            </p>
        </li>
        <li>
            <h1>3、0~10的随机整数?</h1>
            <button onclick="click3()">点击查看</button>
           
            <p id="p33"></p>
            <p id="p3"> 
                
            </p>
        </li>
        <li>
            <h1>4、100~1000的随机整数?</h1>
            <button onclick="click4()">点击查看</button>
            <p id="p4"> 
                
            </p>
        </li>
        <li>
            <h1>5、生成10个不重复的随机整数?</h1>
            <button onclick="click5()">点击查看</button>
            <p id="p5"> 
                
            </p>
        </li>
        <li>
            <h1>6、100~1000之间生成10个不重复的随机整数,并且降序排列?</h1>
            <button onclick="click6()">点击查看</button>
            <p id="p6"> 
            </p>
        </li>
        <li>
            <h1>7、0~5之间随机整数(包含5)?</h1>
            <button onclick="click7()">点击查看</button>
            <p id="p7"> 
            </p>
        </li>
    </ul>
</body>
</html>```

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54551
推荐阅读
相关标签
  

闽ICP备14008679号