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 Review Working with Numbers

Monika Spielman
Monika Spielman
14,443 Points

Are there two possible correct answers to the dieRoll question?

I ran each of these over 2,000 times and never got a zero value for either one. [I shortened the variable name to dr]

var dr = Math.floor(Math.random()*6 +1);

var dr = Math.ceil(Math.random() *6);

Are they both right answers, even though the second was marked as wrong?

1 Answer

Colin Bell
Colin Bell
29,679 Points

Just because you never got a zero in your tests doesn't mean it will never happen.

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. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

So while it is very highly unlikely that exactly zero will be returned, it is possible.

Math.floor(Math.random()*6 +1); is the only correct answer to that question because it will absolutely never return a zero.