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
Christopher Jolley
8,253 PointsHaving a hard time understanding Math.random() logic
var minNum = prompt("Please enter a starting number.");
var maxNum = prompt("Please enter an ending number.");
minNum = parseInt(minNum);
maxNum = parseInt(maxNum);
var ranNum = Math.floor( Math.random() * (maxNum - minNum) + 1) + minNum;
document.write("Your random number between " + minNum + " and " + maxNum + " is " + ranNum);
Specifically, with the line of code below:
var ranNum = Math.floor( Math.random() * (maxNum - minNum) + 1) + minNum;
I know this function is correct because I cheated a bit and looked at the answer. blushes I don't understand WHY this works, though. Something about how the starting number is determined just isn't clicking with me. Let's put some numbers in the function.<br>
minNum = 10 maxNum = 40
Step 1: Math.floor( Math.random() * (40 - 10) + 1) + 10
Step 2: Math.floor( Math.random() * (30) + 1) + 10
Step 3: Math.floor( Math.random() * (31) ) + 10
Step 4: Math.random () * 31 is calculated
Step 5: The number is rounded down in Math.floor()
Step 6: 10 is then added to the result.
Step 7: ???
Step 8: Profit :P
There may be a flaw in HOW I'm thinking about this. Would someone please explain the logic here, because I seem to understand the math just fine.
EDIT: For clarity's sake let me reiterate the question. Consider this:
Math.floor( Math.random() * (maxNum - minNum + 1)) + minNum;
Why is it that this formula guarantees a number between minNum and maxNum? That's where I'm lost.
Christopher Jolley
8,253 PointsThanks for pointing that out; you are right, but that's not quite what I mean. I'm trying to understand how the function is choosing a random number between minNum and maxNum without accepting two arguments. It should make sense because the math is there, but for some reason it isn't clicking and it's driving me nuts.
Jason Anello
Courses Plus Student 94,610 PointsHi Christopher,
I formatted the code to make it easier to read.
You do have a slight error in your formula.
The + 1 needs to be inside the parentheses with maxNum - minNum. Otherwise you'll be getting 1 less number from this than you intend to.
var ranNum = Math.floor( Math.random() * (maxNum - minNum + 1)) + minNum;
Jason Anello
Courses Plus Student 94,610 PointsI noticed you made an edit to your question. We don't receive notifications on edits so it's best to leave a short comment saying you've done so.
Did Deepak's answer clear it up for you or do you need a different approach?
Christopher Jolley
8,253 PointsDeepak Sharma's explanation isn't registering which me either. I may be missing something on a fundamental level. I understand how the Math.random() function works by picking a number between 1 and ONE user-generated number. I am not understanding how the program is choosing a random number between TWO user-generated numbers.
2 Answers
Keith Corona
9,553 PointsMath.random() selects a random number from 0 - .9999 so when you enter a max number and a min number it is multiplied by that random number. Add in 1 since you are using math.floor to eliminate the chance of rounding to 0 and then adding in the min entry again to get somewhere in between the two numbers.
Deepak Sharma
13,345 PointsLet me try one more time with the steps you have mentioned.
Step 1: Math.floor( Math.random() * (40 - 10) + 1) + 10
Step 2: Math.floor( Math.random() * (30) + 1) + 10
Step 3: Math.floor( Math.random() * (31) ) + 10
Step 4: Math.random () * 31 is calculated
Math.random() can return a value between 0[inclusive] and 1[exclusive] so the interval that can be returned by Step 4
will lie between 0[inclusive] and 31[exclusive], actually it would be 31 * 0.9999...... . What matters to us is that the
maximum value Math.Random() * 31 will always be less than 31
Step 5: The number is rounded down in Math.floor()
Applying Math.floor() on interva 0[inclusive] and 31[exclusive]. Will return a number between 0 and 30 because Math.floor(x) = m if and only if m <= x < m+1. Highest Value that our interval can return is always less than 31 so 30 <= highest_value < 31. Apllying floor function on it can return a maximum value of 30. So the interval this function can return is [0] inclusive and [30] inclusive or in other words [0, 30].
Step 6: 10 is then added to the result.
Adding 10 to this interval [0,30] = [0+10, 30+10] = [10, 40]
Step 7: You get a from interval [10,40] which is what you wanted.
Step 8: Profit :P
Jason Anello
Courses Plus Student 94,610 PointsHi Deepak,
You have an error in step 3. When you add 1 to the interval the result is going to be [1, max-min+1)
You may want to update your steps to reflect the correct formula as shown in the mdn link.
Also, your first link goes to this question. Was it supposed to be a link explaining interval notation?
Deepak Sharma
13,345 PointsCorrected the errors and link Jason Anello. Please check
Keith Corona
9,553 PointsKeith Corona
9,553 PointsIt looks like, to me, that in your logic, step 3/4 is off. It should be math.random() * 30 and that result is then taken and + 1 is added -- meaning, it wouldn't be math.random()*31.
Following that, it would then be rounded down with math.floor(), and finally the + 10 added. Hope that helps.