Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

Ugur Aydin
Ugur Aydin
5,324 Points

Please explain this to me (Math.random related)

I don't get how (code between quotes);

var randomNum = Math.floor( Math.random() * "(userNum2 - userNum1 + 1)" ) + userNum1;

starts the random number generator to start from the users input.. can someone explain the math behind this to me please.

Heres the full code incase it's needed.

var userNum1 = parseInt(prompt("Enter the first number")); var userNum2 = parseInt(prompt("Enter the second number")); var randomNum = Math.floor( Math.random() * (userNum2 - userNum1 + 1) ) + userNum1; var message = "<p> " + randomNum + " has generated between the numbers " + userNum1 + " and " + userNum2 + " <p>";


7 Answers

David Gardner
David Gardner
10,143 Points

Math.random() returns a decimal number from 0 up to, but not including 1. So anywhere from 0.00000 to .999999999.

Alright let's assume our numbers are 5 and 10, so the possible numbers we want to return are 5, 6 ,7, 8, 9, and 10. The "userNum2 - userNum1 + 1" gives us the range our numbers can be in. In our case the range is 10 - 5 + 1 = 6. There are 6 numbers [5-10] in the possible numbers we can return so this matches up.

Now if we multiply Math.random()*6 we will get a number from 0.0000 to 5.99999. When we apply the Math.floor() our range then gets rounded down to 0 to 5. Alright we have a random number 0 to 5 but we want a random number from 5 to 10. This is where adding the userNum1 or 5 in our case, comes in.

If we add the bottom most number, 5, to our random number from 0 to 5 we get a number from:

5+0 to 5+5 = 5 to 10

Exactly what we want!

Ugur Aydin
Ugur Aydin
5,324 Points

I think im getting it, thanks for the explanation!

Thank you! This is the kind of explanation I needed. :)

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

But I suppose this code won't work when the first number has the higher value than the 2nd number.

(userNum2 - userNum1 + 1)

this will give a negative number!

Ugh.. I just feel like I'm having a hard time with this math code stuff idk why. I just find myself staring at the screen trying to piece it together and my brain just freezes on me. I'm not very gifted at math..

It's ok Vincent, it took me more than a few times looking at the statement over and over to understand it. I feel like the syntax is part of what makes it tricky, at least for me. You can do this! :)

So let's try to break it down in pieces, and look at each piece one at a time in detail. I'll use user inputs 6 and 10 as examples. Take your time reading this, and realize I didn't grasp this all very quickly myself! Writing this out helped me solidify even more. So I hope this helps you! And if I have errors I hope anyone can help correct.

For example, we have Math.floor(lots of stuff in here) + userNum1.

Let's first look at Math.floor(stuff) and ignore the userNum1 for now.

Some of that "stuff" inside is Math.random(). That's going to generate a random number. Keep in mind this Math.random() is giving us a decimal number which can be 0, up to but not including 1. So, we will need to turn that into an integer, and it needs to makes sense according to the user's inputs. We need to do some math for that.

So let's look at the userNum2 - userNum1 + 1. It answers an important question:

  1. How many possible numbers can we generate for the user that make sense? For example, if they give us 6 and 10 we can only choose from 5 possible numbers to give them. There are 5 numbers in their range.

So to find out how many possible numbers, we just subtract the smaller number from the larger number, then add 1. We need to add 1 since 10-6=4, and there are actually 5 possible numbers between 6 and 10. That's why you see userNum2 - userNum1 + 1 in this statement.

So now, the Math.random() is set to give a random number, which is then multiplied by the total possible numbers in the range. We multiply here because our Math.random() gave us a decimal number below 1. If you look at my example of 6 and 10, when we tell the Math.random() generated to multiply itself by 5, we are only going to get number as small as 0 and as large as 4.something (like 4.9999). It won't ever give us a number as high as 5 after multiplying by 5, since it can only ever give us an original random value as high as .9999.

Once that multiplication occurs, we are left with Math.floor(a number) + userNum1.

Math.floor takes the decimal number we have between 0 and 4.9999, and rounds it down to the nearest whole number. So now we are going to have something between 0 and 4. But we want the user to see a random number between 6 and 10, remember?

So, we are adding userNum1 (which is 6) at the end, because to make a randomly generated 0 into a possible 6 output, we need to add it to that lowest range number the user gave us. Adding that lowest range number to a randomly generated 4 (as that's the highest we can get with our math above), will give us 4+6=10, returning the highest possible number that makes sense for the user's inputs of 6 and 10.

I hope this helps! :)

This Math.random is confusing, let me put this in my word and make sure I understand, someone please check my words LOL,

var randomNum = Math.floor( Math.random() * (userNum2 - userNum1 + 1) ) + userNum1

so (usernum2 - usernum1) + 1 # now we know how many numbers in total (math.randon () ) # we generate a random number with decimal (math.floor) # to round down to a integer +userNum1 # so that we have the random number in the range between num2 and num1

Thanks so much for breaking it down for me! Really a huge help!!

If this reply is for me, you're welcome!! :) I'm glad to hear!

indeed it is for you, Janelle!