Home > front end >  How to generate random integer 0 - N?
How to generate random integer 0 - N?

Time:04-17

Zero is the minimum, N is a maximum, N is given, and a algorithm

CodePudding user response:

Math. Round ((Max - min) * Math in the random () + min)

CodePudding user response:

Function the random (n) {return math.h round (Math) random () * n)}

CodePudding user response:

habits, prefer to use Math. The floor

CodePudding user response:

reference illiterate old gu reply: 3/f
habits, prefer to use Math. The floor


Because Math. The random () returns a greater than or equal to zero, but a random number less than 1, if use Math here. The floor down integer, the maximum number will never appear,
Take between 0 ~ 5, unless you want to get the number is not contain 5, otherwise is not allowed to use Math. The floor
  • Related