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.ceil

Why would Math.ceil return 0? if the highest number Math.random returns is 0.99999 then when you ceil that shouldn't it be 1? When you Math.ceil (4.999), the answer is 5, so why would the highest number generated by Math.random() be ceiled to 1? - Math.ceil(Math.random());

6 Answers

Math.ceil(): This function (get it, the ceiling function) always rounds up, so 3.1 and 3.8 both end up as 4. hope this helped :)

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It's not so much that it will return a 0, but that it could. Remember, the Math.random() function can result in a 0, but it's very unlikely. And Math.ceil(0) will result in a 0. Hope this helps! :sparkles:

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.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Dennis Amiel Domingo! The problem with your interpretation is that it overlooks what happens when Math.random actually rolls a 0. And when I say this is a low probability, I'm talking like one in several million. Not one in 10. But eventually, it will roll a 0 and all of a sudden your dice roll will go from 2 to 7 to 1 to 7 and you now have an extra number unaccounted for. This can be particularly disastrous if using these random numbers to access arrays and you now end up with an out of bounds error that causes your app to crash.

Hope this helps :sparkles:

Yeah, I forgot about the 0 + 1 scenario. Thanks!

raquel pinto
raquel pinto
12,549 Points

Well, I think it could work if you type:

Math.ceil(Math.random() * 5) + 1, no ?

I mean, it would still get from 1 to 6, no ?

Boban Talevski
Boban Talevski
24,793 Points

Regarding raquels's answer. Yeah, but this means that to get 1, you need Math.random() to return 0, which is one in a zillion chance so to speak. So it's not really a random chance to get 1 to 6, it's more like a random chance to get 2 to 6 with a very very low chance of getting a 1 and most of the time it's not what we would want.

Unless it's some type of a lottery app when we want some particular number to have a very low chance of showing up. Like maybe if you want to give a prize to some visitor on your site whenever the random function returns 1. And depending on the number of daily visitors, it could still take years I guess :).