Generate Random Whole Numbers within a Range 2

I can’t seem to understand how myMin is added to each random whole number that is generated from Math.floor(Math.random() * (myMax - myMin +1)) + myMin;

for example:
How is Math.floor(Math.random) * ((15 +1) -5)) + 5
= 0+5, 1+5, 2+5, 3+5, etc.?
-How is 5 added to each whole number?
-How does it know when to stop adding 5 to each whole number?

Thank you for the answer!
just one question, i did the formula correct the only thing i didn’t understand is why +1?
if i want for example the numbers 5,15.
15-5 is 10, then you make a random number between 0-10, then i add it to the min number (off course with the Math.floor()).
this is the line i wrote:
return Math.floor(Math.random()*(myMax-myMin)) + myMin;
the output should be a number between 5-15, why do i need the “+1”?
Thank you!

These are fantastic replies! Explains the code fully!

Math.rand starts at (and includes) 0. Basing a numbering system from 0 creates numerous Off By One Error (O.B.O.E.) bugs, and is something you will have to constantly be on the lookout for - it’s the same reason you have to look up index 0 in an array to get the first element.

While not directly related to array indecies, it’s a great example of having to look at number differently. Wikipedia has some good info on this topic:

1 Like

ohh, ok, thank you very much for the help and the precise answer :slight_smile:

Thank you for the heads up! :slight_smile:

1 Like

Great explanation, @camperextraordinaire!
I had found another source which explained this similarly, and it was an eye opening, ah-ha moment :slight_smile:
In hopes that someone else will glean a little insight from it being explained a little differently, I’ll give it a try…

2 things are at play here:

  1. Math.random( ) will return a value between 0.0000… and 0.999999… In other words, between 0 (inclusive) and almost 1 (but never 1).
  2. Math.floor always rounds DOWN (to the floor). So, anything right of the decimal point will just be lobbed off.

So… This means that, Math.floor will round down (Math.random( ) to 0, giving us only 1 possible integer for an answer - zero:
0.0…1
0.0…2
…etc, etc, all the way to
0.999999…
all of which would round DOWN to 0, once Math.floor knocks off everything right of the decimal point.

If, however, before we let Math.floor do its job, we multiply Math.random by any whole number, it will increase the number of integers it returns by that number. If we want a range of 10 numbers, we would multiply Math.random by 10, and it will return a random number between 0 and 9.9999999, providing 10 possible whole numbers (0,1,2,3,4,5,6,7,8,9).

If we multiply Math.random’s output by 30, we get 30 whole numbers (0 - to 29).
and on and on…

Looking at the formula again, the number of integers we want back is defined by the spread of (myMax - myMin)+1. For example myMax(100) - myMin(50), would give us a spread of 51 integers (inclusive of 50 and 100 and the other 49 numbers in between), which is why we add the 1 to it…because 100-50=50, but there are actually 50+1 integers (50, 51…100). So we add the 1, to make sure we are including the “0” number, which is the “50” in the case of 50 to 100.

We add the myMin on to the end, because we want our range of numbers not necessarily to start at “0” but at the myMin number (which would be at 50 in the example above).

I hope this helps, and that I haven’t made it “clear as mud” :slight_smile:
Best wishes!

2 Likes