current position:Home>[javascript] Method for generating random numbers in certain intervals based on Math.random

[javascript] Method for generating random numbers in certain intervals based on Math.random

2022-11-24 23:08:36Evenurs

一、简介

Math.randomThe generated result is onedouble型数据,The lower limit of its range is 0.0,The upper limit is infinitely close1.0.即[0,1). 

二、需求

Get a random number within a range.

三、方法

使用Math下的random随机数生成函数.

四、Interval values ​​are practically applied

4.1浮点型

4.1.1[0,1)

代码如下:

function frandom(){
    return Math.random()
}

4.1.2[0,n)

代码如下:

function frandom(n){
    return Math.random()*n
}

4.1.3[m,n)

代码如下:

function frandom(m,n){
    return m+Math.random()*(n-m)
}

4.2整型-[x,y]

4.2.1[0,1]

代码如下:

function frandom(){
    return Math.floor(Math.random()*2)
}

4.2.2[0,n]

代码如下:

function frandom(n){
    return Math.floor(Math.random()*(n+1))
}

4.2.3[m,n]

代码如下:

function frandom(m,n){
    return Math.floor(m+Math.random()*(n-m+1))
}

4.3整型-[x,y)

4.3.1[0,1)

代码如下:

function frandom(){
    return Math.floor(Math.random())
}

4.3.2[0,n)

代码如下:

function frandom(n){
    return Math.floor(Math.random()*n)
}

4.3.3[m,n)

代码如下:

function frandom(m,n){
    return Math.floor(m+Math.random()*(n-m))
}

4.4整型-(x,y]

4.4.1(0,1]

代码如下:

function frandom(){
    return Math.floor(Math.random()+1);
}

4.4.2(0,n]

代码如下:

function frandom(n){
    return Math.floor(Math.random()*n+1);
}

4.4.3(m,n]

代码如下:

function frandom(m,n){
    return Math.floor(m+Math.random()*(n-m)+1);
}

4.5整型-(x,y)

4.5.1(0,1)

代码如下:

function frandom(){
    return undefined;
}

4.5.2(0,n)

代码如下:

function frandom(n){
    return Math.floor(1+Math.random()*(n-1));
}

4.5.3(m,n)

代码如下:

function frandom(m,n){
    return Math.floor(m+1+Math.random()*(n-m-1));
}

五、总结

Due to the specificity of the endpoint,Hence in order to matchrandom的生成机制[0,,1),Make sure that the probabilities are theoretically as stable as possible,仅使用Math.floorRestrict integer data.至于为什么不用ceil或round,在文章“【python】关于Math.floor、Math.ceil以及Math.roundDiscrimination analysis applied to random numbers_Evenurs的博客-CSDN博客”中分析.

附录 测试用html5源码

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">

    <title>Simulate Tools-By_Even-2022-7-25</title>
    <style type="text/CSS">
        #header{
            background-color: black;
            color: white;
            text-align: center;
            padding: 5px;
        }
        #nav{
            line-height: 30px;
            background-color: #eeeeee;
            height: 300px;
            width: 100px;
            float: left;
            padding: 5px;
        }
        #section{
            width: 800px;
            float: left;
            padding: 10px;
        }
        #footer{
            background-color: black;
            color: white;
            clear: both;
            text-align: right;
            padding: 5px;
        }
    </style>

    <script type="text/javascript">

        function funcpsbinit() {
            document.getElementById("inptimes").value = 0;
            document.getElementById("idtaresult").value = "NULL";
        }
        function funcrandom(base) {
            return Math.floor(Math.random() * (base + 1));
        }

        function frandom() {
            return undefined;
        }
        function floop() {
            var texts = "";
            var cnt = 0;
            var val1 = 0;
            var val2 = 0;
            while (1) {
                if (1 == frandom()) {
                    val1 += 1;
                } else {
                    val2 += 1;
                }
                cnt++;
                if (cnt > 1000)
                    break;
            }
            texts += "1:" + val1 + " 2:" + val2;
            return texts;
        }
        function funcpsbenter() {
            var times = document.getElementById("inptimes").value * 1;
            document.getElementById("idtaresult").value = floop();
        }

    </script>
</head>

<body>
    <div id="header">
        <p>仿真器<b></b></p>
    </div>
    <div id="nav">
        <p>工具<b>1</b></p>
        <p>工具<b>2</b></p>
        <p>工具<b>3</b></p>
    </div>
    <div id="section">
        <table border="1" cellspacing="2" cellpadding="5" frame="both" rules="both">
            <caption>主要</caption>
            <tr>
                <th>刷新次数</th>
                <th>
                    <input type="number" name="points" value="0" min="0" max="999" step="1" id="inptimes">
                </th>
                <th>
                    &nbsp;
                    <button id="psbinit" style="width:inherit" onclick="funcpsbinit()">初始化</button>
                    &nbsp;
                    <button id="psbenter" style="width:inherit" onclick="funcpsbenter()">确定</button>
                    &nbsp;
                </th>
            </tr>
        </table>
        <textarea name="taresult" id="idtaresult" cols="60px" rows="40px">
        </textarea>
    </div>
    <div id="footer">
        Copyright V1.02
    </div>
    <script type="text/javascript">
        readlocaldateoption();
    </script>
</body>

</html>

copyright notice
author[Evenurs],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/328/202211242305595771.html

Random recommended