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!

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

I don't understand how Math.ceil could return zero, if Math.random produces a number between 0 and 1.

Surely if Math.random produces a number between 0 and 1 and Math.ceil is called, any number created would be rounded up to 1.

2 Answers

Hi Chris,

Math.random() can return any number from 0 up to but not including one. It can return zero but not 1.


Nicholas Kim
Nicholas Kim
2,884 Points

I tried this code to see if this was possible. Try running it in your local environment

for(var index = 0 ; index < 1e10 ; index++){ if(Math.ceil(Math.random()) == 0 ){ alert("Found!"); } }

The code basically runs for 1e10 times to see if Math.random() can produce a 0.0000000...