Use of "+1" in random number generator

Hello, in “Basic JavaScript: Generate Random Whole Numbers within a Range” we are taught about a random number generator formula which works this way :

Math.floor(Math.random() * (max - min + 1)) + min


Can somebody explain me what is the use of +1 ?? I have tried removing it and the genarated numbers are the same, i see it makes no difference.

Thanks in advance.

random gives a number 0 <= x < 1

if you want a random number between 3 and 5 you would want to get 3 or 4 or 5, right?
5 - 3 is 2
so the random number times 2 rounded down will give or 0 or 1, added min becomes or 3 or 4
if you instead use max - min + 1 you get 3, so that multiplied the random number gives 0,1 or 2, and adding min it becomes 3, 4 or 5

1 Like

thank you very much!!!