js generates random numbers usually using the Math.random() function of javascript
Several commonly used methods: Math.random() means: the result is between 0-1 (including 0, excluding 1);
Math.floor(Math.random()*10+1) indicates that the result is a random number between 1-10
Math.floor(Math.random()*24) means the result is a random number between 0-23
1.Math.random(); returns a random number between 0-1, may be 0, but always less than 1, [0,1)
The formula to return the range between the specified range of random numbers (m~n) is:
Math.random()*(nm)+m
2.Math.ceil(n); Returns the smallest integer greater than or equal to n (rounded up)
When using Math.ceil(Math.random()*10), it mainly obtains random integers from 1 to 10, and the probability of taking 0 is very small
3.Math.round(n) returns the value of n rounded integer
Use Math.round(Math.random()); to obtain random integers of 0 and 1 in a balanced manner
When using Math.round(Math.random()*10), a random integer from 0 to 10 can be obtained in a basic equilibrium
4.Math.floor(n) returns the largest integer less than or equal to n (rounded down)
When Math.floor(Math.random(*10)) is used, random integers from 0 to 9 can be obtained evenly
…
The post js to generate random numbers first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9890
This site is for inclusion only, and the copyright belongs to the original author.