Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Working With Numbers Create a random number

Math.floor also returns zero...confused.

Hello - In this video, Dave states that you need to use Math.floor(Math.random() * 6 ) + 1 to return a random integer between one and six. He suggests that using Math.ceil(Math.random() * 6) wouldn't work because, on the off-chance it returns a zero, Math.ceil would equal zero.

Math.floor(0) also equals zero, so I'm confused as to why that's his preference.

Britton Zirkle
Britton Zirkle
Courses Plus Student 15,005 Points

Hi Jason,

The reason for adding a 1 is not because of the floor method. The random method will return a number between 0 and 1. In the event it returns 0, 0 would be multiplied by 6 and the number generator will give a final value of 0. In this case, adding the 1 will result in a final value of 1 instead of zero. In the event of random returning a high value just shy of 1 (0.9999...) it would be multiplied by 6 giving a value of 5.99... The floor method would then round it down to 5. We then add the one to give a final value of 6. I hope this helps.

2 Answers

Josh Olson
Josh Olson
13,236 Points

Small correction to Britton's answer,

Math.random() returns a floating number in the range [0,1). That is including zero and anything up to 1, but not 1 itself. This is where the confusion lies. Source.

What you're thinking...

Suppose random did return "numbers between 0 and 1". That is any number from 0 to 1, including 0 and 1. Let's try plugging in these values into the dice roll using ceil and see what happens.

Using the lowest number: 0
= Math.ceil(Math.Random() * 6) + 1
= Math.ceil(0 * 6) + 1
= Math.ceil(0) + 1
= 0 + 1
= 1

Using the highest number: 1
= Math.ceil(Math.Random() * 6) + 1
= Math.ceil(1 * 6) + 1
= Math.ceil(6) + 1
= 6 + 1
= 7

By these methods plugging random numbers between 0 and 1 into the dice roll returns: {1, 2, 3, 4, 5, 6, 7}. Notice the extra number: 7. Since at both extremes you're dealing with whole numbers, ceil and floor would give the same result. We're going to ignore these results because once again, random returns [0,1). Let's try this number range and see how ceil would work.

What really happens...

Using the lowest number: 0
= Math.ceil(Math.Random() * 6) + 1
= Math.ceil(0 * 6) + 1
= Math.ceil(0) + 1
= 0 + 1
= 1

Using a high number: 0.99999
= Math.ceil(Math.Random() * 6) + 1
= Math.ceil(0.99999 * 6) + 1
= Math.ceil(5.99994) + 1
= 6 + 1
= 7

Once again we received 7 possible outputs. The problem was with the upper end and this is where floor and ceil differ. Because we are dealing with 0.999 instead of 1, floor will return 5 and ceil will return 6. That's why we need floor here.

What works...

Using the lowest number: 0
= Math.floor(Math.Random() * 6) + 1
= Math.floor(0 * 6) + 1
= Math.floor(0) + 1
= 0 + 1
= 1

Using a high number: 0.99999
= Math.floor(Math.Random() * 6) + 1
= Math.floor(0.99999 * 6) + 1
= Math.floor(5.99994) + 1
= 5 + 1
= 6

Here we get {1,2,3,4,5,6}. This is what we wanted. Bingo!

Britton Zirkle
Britton Zirkle
Courses Plus Student 15,005 Points

Absolutely Josh. Thanks for the clarification in my response.

Molly Robinson
Molly Robinson
770 Points

This makes perfect sense to me Josh, thank you. What really irks me is HOW the teachers language when talking about .ceil being the method you SHOULDN'T use made it more confusing than it needed to be. (he said it's because it returns a 0? which didn't make sense to me since .floor and .ceil could both do that).

Steven Snary
Steven Snary
17,540 Points

You are correct, Math.floor can also return a value of 0.

1 - That is why the first step is to Multiply the Math.Random by 6 - this will calculate a number between 0 and 5.9999...

2 - We then use the Math.floor to guarantee that we'll return a value of 0 through 5.

3 - Then 1 is added to that number to finally get what we wanted - A number between 1 and 6 - just like a die would roll.

The +1 is the key to this calculation so that we never get a value of 0 and always at least a value of 1 (even if the Math.random method actually gave us a 0 value)

The Math.floor also guarantees that before we add the 1 to the calculation that we are starting with a number that is between 0 and 5 inclusive.

Hope this helps!