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 trialMax Primo
976 PointsSo 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?
3 Answers
Cameron Childres
11,820 PointsHey Max~
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:
- 0.06402171468849871
- 0.0485511979040667
- 0.381454323282967
- 0.2018132157678454
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:
- 0.00000000000000001
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:
- 0.0000000000000000
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.
Alasdair Glenday
4,394 PointsThanks so much Max and Cameron for this! Had me so confused as I was doing the exact same thing.
I understand now, that because Math.random makes a number between 0 and up to but not including 1, there's an incredibly small chance the random number might be 0.0000000000, which even multiplied by 6 and rounded up still equals 0.
Thank you both!
Michael Jacoby
Full Stack JavaScript Techdegree Student 3,793 Pointsconsole.log(Math.round(Math.random() * 6)) did return a 0. I didn't try it with a +1, because I thought it might return a 7.
Seems JS could have been a bit more user-friendly with randomDieRoll() or copy Python with randint and randrange.
Max Primo
976 PointsMax Primo
976 PointsOh I see, thank you for clarifying!
Charlie Bird
7,277 PointsCharlie Bird
7,277 PointsCame here to ask the same question. Thanks for explaining!