This doesnât produce a random integer, it produces 0 every time.
Because itâs not asking for any random integer, itâs asking you for a random integer between min and max. Math.floor(Math.random()) * 10 will just return 0 every single time - random gives you a number between 0 and 1 (not including 1). floor rounds that down to the nearest integer, which is always 0. 0 * 10 is 0.
Math.floor(Math.random() * 10), which is what I think you meant, just gives an integer that is 0,1,2,3,4,5,6,7,8 or 9, thatâs it.
The numbers 0-9 arenât âany random integerâ, theyâre just the integers 0-9.
Say you have a min of 5 and a max of 15. You want a number that is 5,6,7,8,9,10,11,12,13,14 or 15.
The + 1 in max - min + 1 is because random is non-inclusive. It returns a number between 0 and 1, never 0 and never 1.
So for example, if you want the numbers between 0 and 10, including 0 and 10, you could do Math.floor(Math.random() * 11). The + 1 ensures you can get 10 back sometimes: it will never actually be 11 so floor always rounds down to 10 if the unrounded value is something like 10.93412354124.
If you wanted the numbers between 5 and 10, you could do Math.floor(Math.random() * 6), which gives you the numbers between 0 and 5, including 0 and 5. Then if you + 5 to that, youâre going to get the numbers between 5 and 10 rather than 0 and 5
The + min makes sure the lowest possible value is the minimum value.
So you multiply the result of Math.random by the length of the range you want + 1, round down the result to an integer, then add the minimum value.
Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin
Math.floor(Math.random() * (15 - 5 + 1)) + 5
Math.floor(Math.random() * 11) + 5
So say math.random returns 0.5543656546435645.
Math.floor(0.5543656546435645 * 11) + 5
Math.floor(6.098022201079209) + 5
6 + 5
11
Or say 0.999999999999
Math.floor(0.999999999999 * 11) + 5
Math.floor(10.999999999989) + 5
10 + 5
15
Or say 0.0000000000001
Math.floor(0.0000000000001 * 11) + 5
Math.floor(1.1e-12) + 5
0 + 5
5