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

I just don’t get the math :(

I understand how to use function and argument very well but I just don’t get the math Have I skipped a course? Because I don’t remember learning how to do this math... Can someone remember which exact course we’ve learned this? I’ve been tryin to understand it for a while now and I still have some questions.  

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

  1. Why is it necessary to subtract the lower value from the upper value?
  2. Is 1 added to prevent the random value to be 0?
  3. is the lower value added in the end so the final number would be bigger than lower value?

4 Answers

Steven Parker
Steven Parker
229,644 Points

To clarify, the basic formula is:   random * spread + lower

Since the random number goes from 0 to (almost) 1, then the value you multiply it by (the "spread") spreads it out so it covers more numbers. But it still starts with 0, so you add the lower number to raise the result into the intended range.

Now the spread is calculated by:   upper - lower + 1

You subtract the lower from the upper to get how many numbers are in between, and you add one more because you want the target range to include the upper endpoint.

For example, if you wanted numbers between 6 and 9 (inclusive), you would subtract the lower (6) from the upper (9) to get 3, and then add one more to get 4. And 4 is how many numbers are in the range you want (6, 7, 8 and 9).

So if you multiply random by 4 you get some number between 0 and 3. And then when you add 6, it shifts it up into your intended target range, which is between 6 and 9.

Does it make sense now?

Trevor Osterman
Trevor Osterman
4,016 Points

I'm not the original poster, but I had the same question, and yes that makes more sense now. I was confused, however, since I didn't remember covering this previously in this lesson either.

Steven Parker
Steven Parker
229,644 Points

Glad it helped, happy coding!

Jansen van der Spuy
Jansen van der Spuy
3,936 Points

I knew it was simple but kept tripping over it for some reason. Awesome explanation, thank you.

I've tried... where did I go wrong :(((

        var upperNum = prompt("please pick upper number");
        var lowerNum = prompt("please pick lower number");

        function getNumber(upperNum, lowerNum) {
            var randomNum = Math.floor(Math.random() * (upperNum - lowerNum + 1)) + lowerNum;
            return randomNum;
        }

        document.write(getNumber(upperNum, lowerNum));
Steven Parker
Steven Parker
229,644 Points

Your function is fine, but it is expecting number arguments instead of strings:

    var upperNum = parseInt(prompt("please pick upper number"));  // convert to a number
    var lowerNum = parseInt(prompt("please pick lower number"));

OMG I'm such an idiot haha thanks so much for the help I really appreciate it. I don't think I'll ever forget parseInt () now haha

Just to answer your question regarding if the teacher explains this formula in a previous course: yes, he does!

I don't know in what course exactly, but I remember seeing it in JavaScript Basics when he talks about the JavaScripts Math object.