Max Primo976 Points
So I was testing out the Math.ceil and Math.floor. But I never got 0 when I used Math.ceil. Mistake in the video?
So in the video it states that Math.ceil is not a good way to get a random number between 1-6 because you might end up with a zero. However, when I ran Math.ceil(Math.random()*6); I actually never got any zeros and they were all between 1-6.. Is this a mistake in the video? I am just confused. However, I did get 0 when I ran Math.floor(Math.random()*6); without adding the 1. But my point is that you can get 1-6 without adding the +1 if you just did Math.ceil?
Cameron ChildresTreehouse Moderator 11,682 Points
This is not an error, there's just an incredibly small chance to receive a zero value.
When you run Math.random() you get a random number in a range that starts with 0 and approaches, but never reaches, 1. I just ran Math.random() a few times in my browser, here are some results:
Running Math.ceil() on any of these numbers will return 1, since it always rounds a number up to the next largest integer. Now take for example this number:
This will also be brought up to 1 with Math.ceil() -- it's the next largest integer.
You'd get zero if the random number generated was precisely zero:
There's something like a one-in-one-hundred-thousand-million-million chance of this happening, I wouldn't expect you to see it in a quick test -- BUT, it is possible.