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

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.

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

jobbol
seal-mask
.a{fill-rule:evenodd;}techdegree
jobbol
Full Stack JavaScript Techdegree Student 16,610 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!

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!