###### Amanda Cox

3,213 Points# Can someone explain the random number equation?

I am really enjoying JavaScript so far. Everything makes sense until we always inevitably come back to this random number equation. I just can't seem to wrap my head around it. Can someone explain it to me?

The equation is: Math.floor(Math.random() * (6 - 1 + 1)) + 1;

Which from what I understand means: Math.floor(Math.random()*(max-min+1)+min);

Why do we have to do the minus min plus one? Why can't it be Math.floor(Math.random() * 6) + 1

Thanks everyone.

## 3 Answers

###### Steven Parker

173,944 PointsYou __can__ simply the function here since "min" is 1 and it cancels out the +1, and I certainly recommend it.

I think it was just shown that way to illustrate the full formula, which would become more important when you had a different "min" value (for example, if you wanted a random number from 10 to 20).

In the formula, "max - min + 1" represents the *range*, or how many numbers you want to pick from. Using that example, you'd get __20 - 10 + 1__ or __11__ values (when you include both 10 and 20). But when you multiply by 11 and take the "floor" you'll get a number from 0 to 10, so the final part of the formula adds "min" again which in this case would raise the output to from 10 to 20.

###### Amanda Cox

3,213 PointsCan you explain why itâ€™s needed for a different set of numbers? The challenge in this video asks for any 2 numbers, and I want to understand the theory/logic behind it as well.

###### Steven Parker

173,944 PointsI expanded my answer, does it make sense now?

###### Amanda Cox

3,213 PointsYes, thank you!