This article describes how to use Math. floor and Math. random to obtain random integers. For more information, see
Math. random (): Get 0 ~ 1 random number
Math. floor () method rounds a number DOWNWARDS to the nearest integer, and returns the result. (the nearest integer to x is smaller than or equal to x .)
In fact, the returned value is the integer:
Math. floor (0.666) --> 0
Math. floor (39.2783) --> 39
So we can use Math. floor (Math. random () to obtain an integer in the range you want.
For example, from 1 ~ Take a random number within 52:
First Math. random () * 52 // so that we can get a number with a value greater than or equal to 0 and less than 52.
Then add 1: Math. random () * 52 + 1 // now the number is greater than or equal to 1 and <53
Then use Math. floor to get the integer
Final: Math. floor (Math. random () * 52 + 1)
The value range is 1 ~ The random integer of 52.