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

Ignacio Van Humbeeck
Ignacio Van Humbeeck
2,150 Points

Why not simply use Math.round? I test it and it also gives me 6 sometimes

Math.round( Math.random() * 6);

1 Answer

Hi Ignacio,

Math.random function returns a random number between 0 (inclusive) and 1 (exclusive), which means that it may be possible that the function will return something like 0.00001, and if you use this number and multiple by 6. It will return 0.00006.

When you pass in 0.00006 as the argument to the Math.round function, it will actually round down, and since the dice doesn't have a number 0, you wouldn't want to use this even if the probability of getting a 0 is low.

In this case, Math.floor(Math.random() * 6) + 1 would work best.

Hope this helps :)

-Chris