Great explanation, @RandellDawson!

I had found another source which explained this similarly, and it was an eye opening, ah-ha moment

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:

- Math.random( ) will return a value between 0.0000… and 0.999999… In other words, between 0 (inclusive) and almost 1 (but never 1).
- 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”

Best wishes!