To get a random number in JavaScript, we can use the Math.random() method which returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
To generate a random integer between a minimum and a maximum value (inclusive), we can combine Math.random() with some arithmetic operations:
index.tsx205 chars7 linesIn this example, the getRandomInt() function takes two arguments, min and max, which represent the range of possible random numbers. The formula used to calculate the random integer is (Math.floor(Math.random() * (max - min + 1))) + min.
Math.floor() rounds down to the nearest integer, so that the result is an integer in the range of min to max.
Note that the Math.random() method generates a pseudo-random number, which means that the sequence of numbers it generates is predetermined and not truly random.
gistlibby LogSnag