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?

I assume there is more code than just the following and let’s assume you called Math.random correctly with () instead of just ).

Math.floor(Math.random() * (myMax - myMin +1)) + myMin;

Let’s use your example of myMin=5 and myMax=15 to see how this evalutes

Math.floor(Math.random() * (15 - 5 +1)) + 5;

which simplifies down further to;

Math.floor(Math.random() * (11)) + 5;

The above code will yield a single integer between 5 and 15 (inclusive).
The + 5 on the end is why it is always at least 5 and at most 15.

Let’s work through an example of how this will produce a single integer. First, Math.random() produces a number from 0 up to but not including 1. So, let’s say in our particular call to Math.random() we get the number 0.357373

Next, we multiply this random number by 11 and get 3.931103. So now we have:

Math.floor(3.931103) + 5;

The Math.floor will truncate everything after the decimal, so we have:

3 + 5 = 8

The final value of the original Math.floor(Math.random() * (15 - 5 +1)) + 5 is simply 8


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!

Because Math.random() always produces a random number between 0 (inclusive) and 1 (exclusive), your formula without the plus 1 will never produce the number 10. If Math.random() returned the value 1 (which it does not), then you would not need the plus 1. Let’s look at an example. Let’s say Math.random() returns 0.9999999999, then your formula would look like:

myMin = 5;
myMax  = 10;
Math.floor(Math.random()*(myMax-myMin)) + myMin;

The above formula becomes:

Math.floor(0.9999999999* (10-5)) + 5;

which becomes:

Math.floor(0.9999999999*5) + 5;

which becomes:

Math.floor(4.9999999995) + 5;

which becomes:

4+5 = 9

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, @RandellDawson!
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:
…etc, etc, all the way to
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!