Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Miguel Angel Quintana
Miguel Angel Quintana
6,493 Points

Math.floor and Math.random confusion

Im doing the random number challenge and its a bit confusion the me, mostly these two methods. i know what they do but what gets me confused is how it does it . for example i ended up with

function randomNum(lowNum, highNum) {
    var number = Math.floor(Math.random() * (highNum - lowNum + 1)) + lowNum; 
    return number;
}

console.log(randomNum(2, 4));

So in my train of thought it first does(highNum - lowNum + 1) *which in this case would be 3 *
then the Math.random(); is executed and lets say it randomly generates 0.25363904027298134

then 0.25363904027298134 would multiply with 3 to make 0.760917120818944

now here is where i get confused . everything inside the parentheses has ben calculated. Math.random() * (highNum - lowNum + 1)

would Math.floor be executed after that or + lowNum ?

i would assume Math.floor ? but that would round down to 0 right?

Im horrible at explaining myself but i tried my best hopefully you guys can understand what im trying to figure out herer

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I think you've got the understanding perfectly! What's happened here is that you've picked a test number that's too low. So when we multiply the 0.25363904027298134 and then multiply it by three we get 0.760917120818944. Then we take the floor of it which will result in a 0. And then we add the lower bound to it, which in your case, is a 2. So your random number will be 2. But that's totally reasonable.

But let's look at a slightly bigger number. If it had rolled a .4 from the beginning. We'd multiply that by 3 giving us a 1.2. Then we take the floor of that which will be 1. And we add our lower bound. This results in a 3. Still in range.

But now let's look at a number waaaaaaay on the other end of the scale. Let's say it rolls a .9. We take the .9 and multiply it by 3. This results in 2.7. Then we take the floor of it which is 2. And then we add the lower bound to it. This results in 4. So yes, your random results will only ever be between 2 and 4.

And I'm of the opinion that you're understanding it just fine :thumbsup: Hope this helps! :dizzy:

Steven Parker
Steven Parker
216,083 Points

I see Jennifer has already contributed her reliably sound advice. :sparkles:

:point_right: I would only add this - regarding your question about adding the lowNum before or after the Math.floor is performed - that it makes no difference. Math.floor only removes the fractional part of a number, so adding any whole number will increment the value by the same amount either way. It would only make a difference if the number being added had a fractional component of its own.