 # freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range

freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range
0

# Generate Random Whole Numbers within a Range

## Hints

### Hint 1

`randomRange` should use both `myMax` and `myMin`, and return a random number in your range.

You cannot pass the test if you are only re-using the function `ourRandomRange` inside your `randomRange` formula. You need to write your own formula that uses the variables `myMax` and `myMin`. It will do the same job as using `ourRandomRange`, but ensures that you have understood the principles of the `Math.floor()` and `Math.random()` functions.

## Solutions

Solution 1 (Click to Show/Hide)
``````function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
}
``````

#### Code Explanation

• `Math.random()` generates our random number between 0 and ≈ 0.9.
• Before multiplying it, it resolves the part between parenthesis `(myMax - myMin + 1)` because of the grouping operator `( )`.
• The result of that multiplication is followed by adding `myMin` and then “rounded” to the largest integer less than or equal to it (eg: 9.9 would result in 9)

If the values were `myMin = 1, myMax= 10`, one result could be the following:

1. `Math.random() = 0.8244326990411024`
2. `(myMax - myMin + 1) = 10 - 1 + 1 -> 10`
3. `a * b = 8.244326990411024`
4. `c + myMin = 9.244326990411024`
5. `Math.floor(9.244326990411024) = 9`

77 Likes

Thank you for taking the time to walk through this step by step. I had been able to generate a code answer -but couldn’t understand what the actual formula meant. Awesome. Again - thank you.

7 Likes

Yes, thank you for the explanation. It was easy to figure out what was required to pass the challenge, but I wasn’t comfortable passing this without understanding the formula first. This explained it very logically, and I probably wouldn’t have been able to figure it out on my own. – putting this explanation in my notes!

4 Likes

Awesome work here man really appreciate the the simple explanation!

1 Like

Thanks. Now I understand why we need to use Max - Min + 1 instead of Max - Min.

I understand the part until the +1 to make the max inclusive, but I don’t understand the part after that, what is the point of it ?

5 Likes

Thank you for the detailed explanation. There is still one thing that does not add up for me.

How is `((max + 1) - min)) + min` different than `max + 1`?

If you are subtracting `min` and then adding it back in, doesn’t that cancel out and leave you with `max + 1`?

It is different because of the parenthesis. If Max is 6 and Min is 2, you get ((6+1) - 2)) + 2

And THIS is how the computer works it out:

Math.floor(Math.random)((6+1)-2)+2
=Math.floor(Math.random)
(5)+2
= 0+2…1+2…2+2…3+2…4+2
=2…3…4…5…6

10 Likes

Given `var myRandom = randomRange(2, 6);`

Lets start with `Math.floor(Math.random() * (6 + 1))’ because we can agree that it returns 0-6.

However! If `Math.random()` returned `0` then `Math.floor(0 * (6 + 1))` would return 0 which is outside of our given range of 2 through 6 : .

So the next logical step is to add two after all the multiplication has been executed. Which would look like: `Math.floor(Math.random() * (myMax + 1)) + myMin`

However! If `Math.random()` returned `6.99999` then `Math.floor(6.99999 * (6 + 1)) + 2` would return 8! which is outside of our given range of 2 through 6. no good SOOO we have to subtract the min from our max then add one to guarantee that the floor of random * our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a `0` .

Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.

In other words.

The range of 0 through 4 plus 2 is equal to the range 2 through 6.

10 Likes

Given `var myRandom = randomRange(2, 6);`

Lets start with `Math.floor(Math.random() * (6 + 1))’ because we can agree that it returns 0-6.

However! If `Math.random()` returned `0` then `Math.floor(0 * (6 + 1))` would return 0 which is outside of our given range of 2 through 6 : .

So the next logical step is to add two after all the multiplication has been executed. Which would look like: `Math.floor(Math.random() * (myMax + 1)) + myMin`

However! If `Math.random()` returned `6.99999` then `Math.floor(6.99999 * (6 + 1)) + 2` would return 8! which is outside of our given range of 2 through 6. no good SOOO we have to subtract the min from our max then add one to guarantee that the floor of random * our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a `0` .

Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.

In other words.

The range of 0 through 4 plus 2 is equal to the range 2 through 6.

5 Likes

My solution

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

4 Likes

Thank you. This explains it best. I appreciate your explanation. How did the middle (myMax - myMin + 1) from the original formula become ((myMax + 1) - myMin))? This is the part I am confused about.

I’m finding it very frustrating to have to come to the “Hints” section to learn the skills I should be learning in the lesson. If it’s critical to solving the problems, shouldn’t we learn it prior to the problem?

15 Likes

I feel as the same as you. This lesson doesn’t have enough explanation to the code.

1 Like

You may approach it like this:

// 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; // Changed line

}

randomRange();

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

3 Likes

Agreed. Makes you think somethings wrong with you doesn’t it?

3 Likes