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
A X
12,842 PointsMore Advanced Questions (that hopefully can be explained simply) about adding the second lower to the Random Number code
Dave talks about the following code in his video:
Javascript
function GetRandomNumber (lower, upper) {
var random = Math.floor (Math.random() * (upper - lower + 1)) + lower;
return random;
}
Ok so it took me a long time & drawing pictures to fully understand why we are multiplying by the value obtained through the inner most bracket set. I also understand why were're adding the lower (so it doesn't go outside of the bounds of our random number constraints). So I fully understand the formula
However, my problem starts here. If someone plunked me down and said, "Come up with a random number generator" I would've never thought to add the + lower because it only creates a semi-random value as it'll always be somewhat predictable thanks to using + lower. So my first question is when presented with a problem like `create a random number generator' how does one wrap their head around this problem and come up with a viable solution that doesn't break? I'm sure there's a boat load of other ways that we could've created a random number generator...so why was this method chosen over the others? I feel that incorporating Math.floor makes the problem more annoying to work around because you need to account for the possibility of a value that would round down to 0, there must be more efficient ways to create a random number (I just can't conceptualize them yet).
I also can't think through how the + lower consistently and absolutely ensures that we're not going to end up with a number we're not supposed to. So my second question is how do we know that + lower does consistently what it's supposed to do and there aren't things that could break it?
One of the hardest topics for me is math, and programming is also a challenge for me to wrap my head around...so if someone could respond to this post with as simple of a solution as possible that would be appreciated. Thanks for helping to clear this up for me.
3 Answers
jcorum
71,830 PointsHere's a response I wrote for a similar question: https://teamtreehouse.com/community/for-the-statement-mathfloormathrandom-6-1-1-1-why-am-i-subtracting-1-from-6-and-then-adding-1
Kevin Goudswaard
11,061 PointsThe math can be seriously tricky. Especially if you're revisiting algebraic equations at the same time you're learning a new language. The way I always do it (if it helps at all, maybe not) is I start at the beginning of the equation and read left to right. first you're declaring a new variable: "random" then inside of this variable, you are storing an equation.
The first part of your equation, Math.floor, takes the results of all the math inside and rounds down. This is javascript's way of managing the random numbers. (because they are HUGE.. the numbers after the decimal place go practically forever).
After that you have your math.random function. that is what randomizes your contents. then you multiple that by your range. upper and lower are your inputs: (pick two numbers) then you add one because the math.random randomizer could round to 0. you want it to add 1 so your range is from 1 to X, not 0 to X.
Lastly (and the point of your question): What if the result of upper-lower+1 = 0? You are trying to get a number BETWEEN the lower and upper. If you don't add that lower again, there is a chance your generator will produce a number that falls below your range. You want to account for the fact that you will AT LEAST get the minimum number. I really hope that helps. Sorry for the block of text!
A X
12,842 PointsAs I mention in my question, I understand the parts of the formula. That wasn't what I was asking about.
Kevin Goudswaard
11,061 PointsI am afraid then, that I cannot answer your question.
jcorum
71,830 PointsLet's try this then.
Math.floor(Math.random() * (75 - 50)) = Math.floor(Math.random() * 25), which yields a random number between 0 and 24 (not 25!).
Math.floor(Math.random() * (75 - 50+ 1)) = Math.floor(Math.random() * (25 + 1)) = Math.floor(Math.random() * 26), which yields a random number between 0 and 25.
So, basically, you add the 1 because Math.random() * n returns 0 to n - 1.
Now, if you want a random number between 50 and 75, you add 50
If Math.floor(Math.random() * (75 - 50+ 1)) returns 0, then 0 + 50 is 50
If Math.floor(Math.random() * (75 - 50+ 1)) returns 25, then 25 + 50 is 75
So both limits are covered.
A X
12,842 PointsA X
12,842 PointsHi jcorum, Before I posted this question I read the other post, and your answers to that one were based on the person not understanding why we're using this formula to begin with. I get why, I just don't understand how someone would've thought through the logic of the formula in the first place, and then why
+lowerwill consistently give us the results that we want vs. there being "breaking" conditions. So no, you don't answer my questions with the other post.