Generate Random Whole Numbers within a Range-- Is this how range works?

Tell us what’s happening:

Why is range like this:
(myMax - myMin + 1)) + myMin;

We’re subtraction myMax from myMin then adding 1, then adding myMin again? Huh? Is that how range is supposed to be formatted? I don’t get it.

Your code so far


// Example
function ourRandomRange(ourMin, ourMax) {

  return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
}

ourRandomRange(1, 9);

// Only change code below this line.

function randomRange(myMin, myMax) {

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

}

// Change these values to test your function
var myRandom = randomRange(5, 15);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range

Let’s look at this in a few steps while assuming that ourMax is 20 and ourMin is 10.

Step 1: Math.random

Math.random() will return a number between 0 and 1 (but not 1)
What’s worth noting here is that while we can get a “0”, we can’t get a “1”, the highest we can get is a 0.99999999999… and so on.

Step 2: Math.random * ourMax

Multiplying the output by ourMax will technically increase our range from 0-1 to 0-20 (but not 20)

Step 3: Math.random * (ourMax - ourMin)

Let’s remember that ourMax is 20 and ourMin is 10.
Very similar to step 2, it decreases the range back to 0-10 (but not 10), we’ll fix it in a moment.

Step 4: Math.random * (ourMax - ourMin + 1)

Because we’re rounding down using math.floor and because we can’t get the highest number in range as an integer, that means no matter the output, it’ll always be rounded down to ourMax - 1. Because even if we got 9.9999… (while our range is 0-10) it’d get rounded down to 9.
Adding 1 fixes that problem, now the highest value we can receive is 10.999999… and it gets rounded down to 10, which is the desired ourMax.

At this point our range is 0-10, because we’re not modifying the minimum output we can receive despite adding a 1, we’re only modifying the maximum possible number.

Step 5: Math.random * (ourMax - ourMin +1) + ourMin

This is the last step and it makes our random number generator work in an actual range between 2 numbers. Our lowest possible number is now 10 and highest is 20.
Why? Because we’re adding ourMin outside of the Math.floor function:

Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;

The bolded part is what gets rounded down. After we receive that, we’re separately adding ourMin, which means that even if we got a 0, our final value can’t be lower than ourMin, which in our case is 10, same goes for our maximum value which now can’t be lower than 20.

Hope it made things clearer! Cheers :slight_smile:

8 Likes

Assuming min is 10 and max is 15
random numbers will fall in a range of 5 numbers - between 10 to 15 inclusive so 10,11,12,13,14, or 15

(myMax - myMin + 1)

15-10+1
5 + 1 range of 5 numbers (+ the 1 for the flooring operation as in previous challenge)
This gets you random numbers in a range of 0 to 5 inclusive so 0,1,2,3,4, or 5

)) + myMin;

You don’t want a range 0 to 5 though, so you add 10 to get a range 10 to 15
You have shifted your range of 5 ten places to the right so now 10,11,12,13,14, or 15

1 Like

Hi there, this is one of the best explanations I’ve seen on this but I just have one query.

You say the bolded part is what gets rounded down by the Math.floor function Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;

I am just confused how ourmax when it is 20 is ever being asked to get rounded down.

In steps 2-4 ourMax is 10.

In step 5 ourMax is now 20, but the bit that makes it 20 (+ourMin) is not included in the Math.floor function.

Hope that makes sense! Sorry if i have missed something obvious…I am new to coding.

Thanks in advance

  • Kathryn

Now this is a super explanation.

It’s a shame that the related freecodecamp lesson leaves so much unexplained. That is indeed a bit too much to assume if you are a new learner.

Anyway, thank you! =)

the thing is that Math.random() is multiplied by the width of the range
so if the random number is at first 0 or higher but below 1
after you multiply for the range, that if the min is 10 and max is 20 the range is 11 as you want one of 11 numbers (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
so after the multiplication the random number is 0 or more but below 11, so rounded down becomes one of this numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

at this point the minimum is added to obtain the random number in the desired range