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 trialMickey Odunikan
7,392 PointsThis script never produces a 0 how is it wrong? Math.ceil(Math.random() *6)
This script never produces a 0 how is it wrong? Math.ceil(Math.random() *6)
2 Answers
KRIS NIKOLAISEN
54,972 PointsYou should use floor not ceil
Mikołaj Gonciarz
8,078 Points.ceil()
method rounds up to the closest higher number e.g 0.01 i rounded to 1
.floor()
method rounds up to the closest lower number e.g 0.01 i rounded to 0
Since .random()
method return a random number between 0 (inclusive) and 1 (exclusive) there is actually a possibility 0 will be returned. Yet if you want to be sure that you will get a number from 0 to 6 inclusive on both end of range, with equal probability than I suggest:
Math.floor(Math.random()*7)