Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

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...