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

A line of code

function getRandomNumber (lower, upper = 100) { if ( isNaN(lower) || isNaN(upper) ) { throw Error('Both arguments must be numbers'); }

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

}

This is a function that creates a random number i understand it all except for one part. Math.floor(Math.random() * (upper - lower + 1)) + lower; . This part is is the part i don't understand, What does it do? Why does upper and lower go in those parts? I don't get it

2 Answers

Let me paraphrase your initial question:

What does

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

do? Why does upper and lower go in those parts?

OK, very basically, this is a way to generate a random number between two numbers that you put into it as upper and lower limits, so tat the random number won't fall outside those limits.

By the way, the upper = 100 in the function's parameters sets a default value, so if the upper bound you want is 100, you only have to put in a lower number when you call the function. If you want another number as upper, you just put it in along with lower.

If this doesn't help, go back through my original reply line-by-line and make sure you understand each line. If you find one you get hung up on, let me know.

Best wishes.

Alright thank you for the help

Hi Haki,

I have a basic understanding of this, so I'm going to try to explain it as best as I can. Let's 'do the math' as they say.

As you probably already know, Math.floor() returns the largest integer less than or equal to a number.

Math.random() returns a float between 0 and 1 (including 0, but not including 1).

And, of course, upper and lower are the two numbers you want to generate a random number between, as in "give me a number between 5 and 80".

So we have:

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

Let's attack this with a couple of examples; an upper of 100 and a lower of 1, and an upper of 80 and a lower of 5. In both cases let's check the most extreme cases for the random number, which would be 0.0 and 0.99 (for simplicity).

in the case of (100, 1), Math.random being 0.00, ( Math.random() * (upper - lower + 1) ) would equal 0.00, and Math.floor(0.00) + 1 equals 1.

in the case of (100, 1), Math.random being 0.99, ( Math.random() * (upper - lower + 1) ) would equal 99.0, and Math.floor(99.0) + 1 equals 100.

in the case of (80, 5), Math.random being 0.00, ( Math.random() * (upper - lower + 1) ) would equal 0.00, and Math.floor(0.00) + 5 equals 5.

in the case of (80, 5), Math.random being 0.99, ( Math.random() * (upper - lower + 1) ) would equal 75.24, and Math.floor(75.24) + 5 equals 80.

I think I've got the math worked out properly here, and I hope these examples are making sense to you. Work out a few on your own, with different values for the random float, and different uppers and lowers using the pattern above.

I hope this helps (I think I understand it better myself now).

If I got anything wrong, I hope the Community will chime in with clarifications and corrections.

Thanks for the help but i still don't understand it. I just started js not too long ago so a more simple explanation would help me more