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

Edouard Reinach
Edouard Reinach
2,067 Points

Why do you floor() + 1 instead of ceil() the Math.random() value?

The title pretty much sums it up, I'm wondering if there is any special reason to round down and then add 1 instead of just rounding up

Edouard Reinach
Edouard Reinach
2,067 Points

thanks! Interesting edge case case :)

Jeremiah Lugtu
Jeremiah Lugtu
9,910 Points

It was addressed at 4:40 of the video

9 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Yes, because the Math.random() can generate any number in range [0, 1), there's this edge case where the random number is 0.

  • Math.ceil(0) = 0
  • Math.floor(0) + 1 = 1

This is reason why we need to use Math.floor() + 1 instead of Math.ceil() here, when rolling a dice, you never ever supposed to get a 0 as outcome.

Ahh. I didn't consider that. Thanks William!

Thank you William. I have been stuck on this question for a while and you made it clear.

Hi Edouard,

Consider the following example:

var num = Math.ceil( Math.random() * 4 );

After the code executes, num will be equal to 1, 2, 3, or 4. However, there is a very slight chance that the function Math.random() will return a value of 0. If that happens, then num will be equal to 0. This could cause an error in your program because a fifth random number (zero) may be generated, even though you intended to create only four random numbers.

Joel Pendleton
Joel Pendleton
19,230 Points

You could use Math.ceil( Math.random() * 5 ) + 1 as well as Math.floor( Math.random() * 6 ) + 1

Marko Paasila
Marko Paasila
6,313 Points

Will Math.ceil( Math.random() * 6 ) + 1 return 7 if Math.random() returns 1? I mean isn't the possibility to recieve 1 as a return value from Math.random() as great as recieving 0?

William Li
William Li
Courses Plus Student 26,868 Points

No, Math.random() never returns 1, ever.

From MDN

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Madison Braswell
Madison Braswell
849 Points

Is the answer that - both Math.ceil and Math.floor may return a zero, but you are able to add +1 to Math.floor to remain in the allocated guessing frame whereas adding +1 to Math.ceil may increase the number to beyond guessing frame? And the workaround would be multiplying Math.random by one less than the upper number if using Math.ceil and then adding one?

Madison Braswell
Madison Braswell
849 Points

Found the answer: https://www.codecademy.com/forum_questions/4f977513839b6d0003005464

Using the upper - 1 workaround with ceil: "In this case, the range of values that maps to each output is different. In particular, the chance of getting a 0 (i.e. rolling a 1) is very tiny."

I'm a noob so pardon if my interpretation isn't correct. I guess the reason Math.ceil() is not advisable to use for a 6-dice roll is because there is a chance that it will return a 7? For example, if Math.random gives us 5.1, then that will be 6 right? Now when you perform the Math.ceil(5.1) + 1, that would give you 7, and 7 is not included in a 6-dice roll. Also, even if you get a 0 from Math.ceil, remember that it will be added to 1 right, so a Math.ceil(0) + 1 will give you 1, which is still inside the 6-dice roll.

I tried Math.ceil(Math.random() * 6) + 1; and it always gives me rolls between 2 and 7.

So to keep it simple, Math.ceil gives you a dice roll range of 2 to 7.

Hope that helps.

From my understanding, there is no difference of using either or.

Math.floor() + 1 does the same thing Math.ceil() will do.

I hope this helps.