Method and Example of Generating Random Numbers in js

  • 2021-07-12 04:35:48
  • OfStack

By js The basis for generating 1-tangent random numbers is Math.random() This method is quite special, and the generated random numbers are in the interval of [0, 1]. If you perform one operation, js Only one interval like [n, m] can be generated, which is closed left and open right. Therefore, when there are 1 special requirements, it is necessary to carry out 1 other operations. The following is a simple analysis of various requirements:

Generate integers in arbitrary intervals

1. Fully closed interval [n, m]

This is the most common and well-known long list of formulas: Math.floor(Math.random()*(m-n+1))+n; Is the method of generating this fully closed interval. When it comes to this formula, many people know it, but few people really want to understand it. Mr. Wang forms an interval of [0, m-n+1], which is closed left and open right, and then uses Math.floor() Take any integer between [0, m-n] (it is very important to understand this step), and then add the left endpoint of the interval to become any integer between [n, m] to achieve the goal.

Speaking of this place, there is 1 point must be mentioned 1, casually search 1 under js to generate random numbers, there are many articles will be used Math.ceil() Or Math.round() These two methods, such as generating arbitrary integers in the fully closed interval [n, m], Math.ceil(Math.random()*(m-n))+n; Or Math.round(Math.random()*(m-n))+n; I feel that random numbers, the most important thing is the word random, and the probability of each value must be equal. This point is very important for some specific occasions, such as lucky draw (there is a lucky draw at the annual meeting).

Math.ceil() The problem is n < < m ≈ x, x is the number except the endpoint. If the interval is large enough, n can hardly be obtained, and the probabilities of m and x are almost equal. Because m can't be obtained, the probability is relatively small by 1 point. Math.round() The problem is ES46 EN ≈ ES47 EN = ES48 EN/2. The reason is similar to the previous one. If you don't understand it, you can draw a coordinate axis by yourself, which is very clear.

2. Fully open interval (x, y)

In fact, as long as you remember the above fully closed interval, the opening and closing of all other intervals can be pushed from it, and the process is as follows:
(x, y) = = [x+1, y-1]; That is to say, n=x+1; m=y-1; Substitute it into the above formula to get: Math.floor(Math.random()*(y-x-1))+x+1;

3. Left closed and right open [x, y)

Similarly, [x, y) = = [x, y-1]; Substitution yields: Math.floor(Math.random()*(y-x))+x;

4. Open left and close right (x, y)

(x, y) = = [x+1, y]; Substitution yields: Math.floor(Math.random()*(y-x))+x+1;

Generate floating-point numbers in arbitrary intervals

This kind of practice is used less, but it is also quite interesting.

[n,m)

This is the simplest, because it keeps 1 with the characteristics of random. Math.rondom()*(m-n)+n;

Because of this characteristic of random, it is difficult to get floating-point numbers in other intervals. It takes 1 judgment to meet the requirements. The thought is the same as the integer above. The code is as follows:


    function fullClose(n,m) { //[n,m]
      var result = Math.random()*(m+1-n)+n;
      while(result>m) {
        result = Math.random()*(m+1-n)+n;
      }
      return result;
    }
    function fullOpen(n,m) { // (n,m)
      var result = Math.random()*(m-n)+n;
      while(result == n) {
        result = Math.random()*(m-n)+n;
      }
      return result;
    }
    function leftOpen(n,m) { // (n,m]
      var result = Math.random()*(m-n+1)+n-1;
      while(result<n) {
        result = Math.random()*(m-n+1)+n-1;
      }
      return result;
    }

Related articles: