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

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

#1

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


#2

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:


#3

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