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) Working With Numbers Create a random number

Karls Yee
Karls Yee
725 Points

I don't know why is adding 1 so that we can have a number between 1 and 6

I don't know why is adding 1 to Math.floor so that we can have a number between 1 and 6

4 Answers

Kevin Gates
Kevin Gates
15,053 Points

Put another way, random includes the lower limit, but not the upper. So Math.Random is greater than or equal to 0 AND less than 1.

So in the Math.random * 6, your options range from 0 - 5.999999999999999999999999....etc.

Your Math.floor on this limits the options then to 0,1,2,3,4,5. You add plus 1, so the options are now 1,2,3,4,5,6.

ken schafer
ken schafer
13,263 Points

this was probably the most helpful comment here to explain. Good Job.

Steven Parker
Steven Parker
231,269 Points

The "Math.floor" function is essentially a "round down", so any number less than 1 will become 0. You then add 1 so that the final range will start at 1 instead of 0.

If you don't add the 1, you'll have a random number between 0 and 5.

what if you get 6, why then is it the 1 not added? because we asked for a number up to 6 and therefore it can't go up anymore, it doesn't make sense or know how to explain.

Steven Parker
Steven Parker
231,269 Points

You can only get 6 because 1 is added. When you multiply random() by 6, and then use the "floor' function, the result will be something from 0 to 5 since random() by itself is always less than 1.

I was confused by this as well. After going through the code, my notes and watching the video a few times, I finally understood it.

This line: Math.floor(Math.random() * 6); -this will give you a random number from 0 upto but not including 5.

However, the instructor wants a random number like a die/dice throw, which is a number from 1 to 6. Not adding the +1 at the end, again, produces a number from 0 to 5.

Adding the +1 allows us to achieve the objective of a random number from 1 to 6 because a random number of 0 has the 1 added to it to produce the number 1.

Steven Parker
Steven Parker
231,269 Points

I think you meant to say:

This line: Math.floor(Math.random() * 6); -this will give you a random number from 0 upto but not including 6

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 Points

For instance,

  • Math.ceil(4.5) = 5
  • Math.floor(4.5) = 4

So, 0 <= Math.random() < 1 (check MDN)

  • 0 <= Math.random()*6 < 6
  • 1 <= Math.random()*6 + 1 < 7 In conclusion,
  • 1 <= Math.floor(Math.random()*6) <= 6 (Integer numbers)

  • 0 <= Math.ceil(Math.random()*6) <=6 Because Math.random() can be 0 and *6 still equals zero. So Math.ceil returns zero. Therefore, it cannot be used on making a random dice roller.