Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to generate integers between 1~6 with JS's random number generation function?
How to generate integers between 1~6 with JS's random number generation function?
JS's random number generation function random () combined with other mathematical functions can limit the value of random numbers.

Random number function and correlation function of JS;

math . random(); The result is a random number from 0 to 1 (including 0, excluding 1).

Math.floor (number); The parameter num is a numerical value, and the function result is the integer part of num. ?

math . ceil(n); Returns the smallest integer greater than or equal to n.

Math.round (number); Parameter num is a numerical value, and the function result is an integer rounded by num.

Therefore, the random number of 1-6 can be obtained by using the above function:

1, with math.ceil (math.random () * 6); We mainly get random integers from 1 to 6, and the probability of taking 0 is extremely small.

2. Use math. Circle (mathematics. Random () * 5+ 1), the random integer from 1 to 6 can be basically balanced, and the probability of getting the minimum value of 0 and the maximum value of 6 is less than half.

3. Use math.floor (math.random () * 6+1); , you can get a random integer of 1 to 6 in a balanced way.