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) Creating Reusable Code with Functions Random Number Challenge Solution

Joe Williams
Joe Williams
4,014 Points

If I can't teach it, I know I don't really grasp it. What is actually happening in this function?

I understand that this function is creating a random number between 2 numbers -- but when I actually look at the formula, I don't understand what's happening. Can anyone explain this in a way I can grasp?

function getRandomNumber ( lower, upper ) {
    return Math.floor(Math.random()  *  (upper  -  lower  + 1)) + lower ; 
}

console.log( getRandomNumber ( 1, 6 )); 
console.log( getRandomNumber ( 100, 1000)); 
Joe Williams
Joe Williams
4,014 Points

Where I'm getting confused is, I understand what's happening operationally. Lower and upper are being passed into the function, and console.log is showing the range between 1-6, and 100-1000. But where I get confused is looking at the number. Subtracting the lower number from the higher number, adding +1 to it, multiplying that with a random number, rounding it down ... this is where I'm getting lost.

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Joe;

Let's see if I can be of some help, hopefully more help than confusion anyway. We'll try to look at this step by step mathematically.

Given:

return Math.floor(Math.random()  *  (upper  -  lower  + 1)) + lower ; 

Let's take a look at the second example, 100 upper, 10 lower

  1. Start inside the first set of parenthesis (upper - lower + 1), (100-10+1) = 91.
  2. Multiply that by the Math.random function, which returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive). For the sake of our discussion, let's say that it returns 0.2028994421681174. Times our "randomly" generated number and our range we get approximately 18.46.
  3. Apply Math.floor to that and we get 18.
  4. Add 10 and we get 28.

So why go through all of that is your question, right? The upper and lower variables set the range, multiplying it by random will generate a random number, which multiplied by our range calculation puts it above 0. We add that to our lower variable to make sure that it falls within our range.

Let's look at another example, if our generated number is significantly small, like .00202899. We would get approx 0.1846. Round that down we're at 0, add in our lower variable and our getRandomNumber function would return our lower variable.

Hopefully that helped more than it hindered.

Ken

Hey: did you mean "4. Add 10 and you get 28" ?

Otherwise, this helped me make sense of it, thanks!

Ken Alger
Ken Alger
Treehouse Teacher

Jim;

Yes, I just edited my post to fix that error. Not sure how I got 19 from 10+18 originally. Nice catch.

Ken