freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range

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

#1

We can use a certain mathematical expression to get a random number between two numbers.

Math.floor(Math.random() * (max - min + 1)) + min

See Also: :scroll: Explanation | Explanation in SO

Explanation

Math.random() creates a floating point number between 0 (inclusive) and 1 (exclusive)

Q: Create a random whole number between 2 (inclusive, min) and 6 (inclusive, max) i.e. create a random whole number between min (inclusive) and max (inclusive)

A: Math.floor(Math.random() * (6 - 2 + 1)) + 2

Let’s write Math.random() * max which creates a floating point number between 0 (inclusive) and max (exclusive).

E.g. Math.random() * 6 generates a random floating point number between 0 (inclusive) and 6 (exclusive).



But we need to generate a whole number between 0 (inclusive) and max (inclusive) first. So we do a trick :wink:.

What if we create a number between 0 and max + 1. That way, we also include the max number. And then apply the Math.floor method on the result to get a whole number between 0 (inclusive, min) and max (inclusive).

We can write it as Math.floor(Math.random() * (max + 1)).

E.g. Math.floor(Math.random() * (6 + 1)) generates a random whole number between 0 (inclusive, min) and 6 (inclusive, max).

0 1 2 3 4 5 6

The Math.random() * (6 + 1) part generates a floating point number between 0 (inclusive) and 7 (exclusive).

0..1..2..3..4..5..6..

But that doesn’t solve our given question yet :disappointed:. We need to generate a random whole number between min (inclusive) and max (inclusive). So we need another trick :wink:.



What if we write Math.floor(Math.random() * ((max + 1) - min)) that will generate a whole number between 0 and (max + 1) - min.

E.g. Math.floor(Math.random() * ((6 + 1) - 2)) = Math.floor(Math.random() * 5) that will generate a whole number between 0 (inclusive) and 4 (inclusive).

0 1 2 3 4

But still the question is unsolved :disappointed:. We need the random number between min (inclusive) and max (inclusive).

So we simply write Math.floor(Math.random() * ((max + 1) - min)) + min :stuck_out_tongue_winking_eye:.

E.g. Math.floor(Math.random() * ((6 + 1) - 2)) + 2 = Math.floor(Math.random() * 5) + 2

2 3 4 5 6

Now this answers our given question. Math.floor(Math.random() * (max - min + 1) + min correctly generates a random number between min (inclusive) and max (inclusive) i.e. it generates a random whole number between 2 (inclusive) and 6 (inclusive). :smiley:


I don't get the range concept - Math.random() * (max - min + 1)) + min
#2

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.


#3

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

Awesome work here man really appreciate the the simple explanation!


#5

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


#6

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 ?


#7

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?


#8

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


#9

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 :slightly_frowning_face: : .

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 :slightly_frowning_face:

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

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 :slightly_frowning_face: : .

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 :slightly_frowning_face:

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.


#11

My solution

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


#12

Thank you. This explains it best. I appreciate your explanation. :sunglasses:


#14

How did the middle (myMax - myMin + 1) from the original formula become ((myMax + 1) - myMin))? This is the part I am confused about.


#15

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?


#16

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


#17

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);


#18

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