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

Michael Thompson
Michael Thompson
7,427 Points

I don't get it

I don't get the formula for generating a random number. Like, why do we put +1) + 1? I don't understand what that is doing. Any help?

7 Answers

Ok, so the basic formula

Math.floor( (Math.random() * x) + 1);

creates random numbers between 1 and x. That is clear, right?

Then if you want to create numbers between "bottom" and "top", then you use this:

Math.floor(Math.random() * (top - bottom + 1)) + bottom;

So a random number between let's say 13 and 52 would work like this:

Math.floor(Math.random() * (52 - 13 + 1)) + 13;

If the numbers are fixed and never changing (and you dont need to use a variable), then you could of course directly calculate the result and write

Math.floor(Math.random() * (40)) + 13;

But since we want to be able to change the numbers we leave the full formula.

Makes sense now?

Hi Michael,

the +1 is because Math.random() generates a random number between 0 and 1. In case the generated number is 0 we would get a zero. That's why we add 1. Makes sense?

So when you have the full formula

Math.floor( (Math.random() * x) + 1);

Math.random() creates a number from 0 to 1 multiplied by x (our upper limit) so we get numbers between 0 and up to (but not including) x then we round it down with Math.floor and then add 1 so we don't get a 0 and can also reach the upper number.

Happy coding! Nils

Second one...?

Ok, I have a look at the lesson, don't remember a second one...

Shashikant Chaudhary
Shashikant Chaudhary
5,365 Points

Hey Micheal As you know "Math.random()" function generates a random number between 0 and 1 but not including 1. So whenever you call this function you get something like 0,0.11,0.55,0.99. Now suppose you want to create a dice that generates a random number between 1 and 6. So first thing that you need to do is to multiply the random numbers with 6.

for example

0x6=0

0.11x6=0.66

0.55x6=3.3

0.99x6=5.94

As you can see that the integer part is always between 0 and 5 but we want our result our result between 1 and 6 that is why we add +1 at the end.

Note that "Math.floor()" function is used to get the integer part of the floating numbers.

Hope it helps.

lucky saito
lucky saito
4,257 Points

yah but don't you do bedmass? add before subtraction why is it going in order instead of following bedmass?

Order is actually :

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

The brackets (or parenthesis depending on where you hail from) has addition and subtraction. The order in which they are executed is dependent upon whichever comes first reading from left to right. Technically BEDMAS should be:

B-Brackets E-Exponents D or M - Division or Multiplication A or S - Addition or Subtraction

or....BEDorMAorS. I like BEDMAS better...it doesn't sound like a rejected Pokemon name.

I m reading it like this, say i wanted a number between 3 and 7. when the numbers are put in the formula Math.floor(Math.Random() * (7 - 3 + 1)) + 3; the math in the parenthesis comes out to 5, so it starts at 3 and goes to 5???

Antonija Kasum
Antonija Kasum
1,977 Points

I understand how this works but I would like to know the logical reasoning behind it so I wouldn't have to memorize it... But here is the way to understand how it works: Imagine you need a number between 2-5

console.log( Math.random()); // gives you nuber 0-0.99

console.log(Math.random() * 5); // gives you nuber 0-4.99

console.log(Math.floor(Math.random() * 5)); // gives you number 0-4

console.log(Math.floor(Math.random() * 5) + 1); // gives you 1-5

console.log(Math.floor(Math.random() * 5) + 2); // gives you 2-6 -> when you add lower it increases both the upper and lower result so you need to subtract it from the upper in the next step

console.log(Math.floor(Math.random() * ( 5-2 )) + 2); // gives you 2-4

console.log( Math.floor(Math.random() * (5 -2 + 1) + 2 )); // gives you 2-5

just copy and paste it and play around a bit...

Ali Abbas
Ali Abbas
2,097 Points

Hi Michael Thompson,

I believe I understand your problem with the +1)+1.

Quite simply the +1 in the inner most bracket makes sure that the highest number, which in this case is 52, is attained and the +1 outside the brackets make sure that the number doesn't end up being 0. How does this all work? I'll try my best to explain it as clear as possible.

The formula of math.random as you know gives us a number between 0 and less than 1 (i.e. 0 - 0.99999...). So, if we were to say:

math.floor(math.random * (52 - 13 + 1)) + 13 . first thing to note is BIDMAS comes into this and by using it, we will work this out step by step. Let's say, for example we wanted to work out the highest possible number which in our case is 52. Working out the inner most bracket first by subtracting 13 will give us 39, plus the one gives us 40. Next, we move onto the out brackets. Which requires us to times 40 by the math.random and since we know that random produces any number between 0 and 1, excluding one. For the sake of this example we will say the random number produced was 0.9999, times it by 40 and we get 39.996. Next thing to happen, is the program will use math.floor to round down the number which will equal 39. 39 plus the 13 outside of the brackets gives the final answer which is 52. From here you should be able to see why adding the one was crucial. If the +1 in the inner most bracket wasn't there than 52 - 13 = 39 and if you were to times this by the random produced, 39 * 0.9999 = 38.9961. This would've been rounded down to 38. 38 + 13 = 51. So, your highest number wouldn't have been 52 it would have been 51 if it weren't for the +1.

Now what about the +1 outside the brackets. If the random number produces 0 then whatever you times it by, the end result is 0. The +1 outside the brackets makes sure the number doesn't go below 1. However, in the example it shouldn't go below 13, hence why the number is changed to 13.

I hope this was clear. Take care.