js generates [n,m] random number formula, stores it in an array, and sorts

1. Preliminary knowledge

Math.ceil(); //Round up.

Math.floor(); //Round down.

Math.round(); //Rounding.

Math.random(); //A pseudo-random number between 0.0 ~ 1.0. [Include 0 but not 1] //For example, 0.8647578968666494

Math. ceil (Math.random()*10); // Get a random integer from 1 to 10.  The probability of taking 0 is extremely small.

Math. round (Math.random()); // Random integers from 0 to 1 can be obtained balanced .

Math. floor (Math.random()*10); // Random integers from 0 to 9 can be obtained balanced .

Math. round (Math.random()*10); // Basic equalization gets random integers from 0 to 10 , and the probability of getting the minimum value of 0 and the maximum value of 10 is less than half .

Because the result is 0 from 0 to 0.4, 1 from 0.5 to 1.4...9 from 8.5 to 9.4, and 10 from 9.5 to 9.9. So the distribution range of the head and tail is only half of the other numbers.

2. Realize the function of random rounding function

    Implementation principle: Determine the maximum and minimum values ​​of the random rounding range, the default is 0.

    Use range: generate verification code in js or select an option randomly

//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){ 
    switch(arguments.length){ 
        case 1: 
            return Math.floor(Math.random()*minNum); 
        break; 
        case 2: 
            return Math.floor(Math.random()*(maxNum-minNum+1)+minNum); 
        break; 
            default: 
                return 0; 
            break; 
    } 
}

   Process analysis:

        Math.random() generates the number [0,1), so

        Math.random()*5 generates {0,5) numbers.

        Usually expect to get an integer, so you have to deal with the result.

        parseInt(), Math.floor(), Math.ceil() and Math.round() can all get integers.

        The results of parseInt() and Math.floor() are rounded down.

        So Math.random()*5 generates random integers of [0,4].

        So to generate a random number of [1,max], the formula is as follows:

// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);

         So to generate a random number from [0,max] to any number, the formula is as follows:

// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));

         So I hope to generate a random number of [min,max], the formula is as follows:

// max - 期望的最大值
// min - 期望的最小值
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);

Implement a small demo 

<!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>Document</title>
</head>

<body>
</body>
<script>
    //随机取10-100中三个值,存入一个数组,并排序
    function getRandom(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return Math.floor(Math.random() * minNum);
                break;
            case 2:
                return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
                break;
            default:
                return 0;
                break;
        }
    }
    //声明一个空数组
    var array = [];
    //获取10-100之间的10个随机数,并存入数组
    for (var i = 0; i < 10; i++) {
        array.push(getRandom(10, 100));
    }
    //升序排列
    alert(array.sort(function (x, y) {
        return x - y;
    }));
    //降序排列
    alert(array.sort(function (x, y) {
        return y - x;
    }))
</script>

</html>

The author of this article, Wang Xiaoxiong , is constantly learning and growing due to changes in knowledge itself, and the content of the article is not regularly updated. In order to avoid misleading readers and facilitate the tracing of the source, please reprint and indicate the source.

If you have any questions, please discuss with me and make progress together. 

If you think this article is helpful to you~ You can support it on WeChat:





    

Guess you like

Origin blog.csdn.net/m13302979400/article/details/80637438