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

Eli E
Eli E
10,142 Points

Why does it rarely return 1?

I've rolled 40 times and I only got 1 once.

2 Answers

2 things to consider:

1.) There is no such thing as true random, computers use pseudo-random, human's use perceived random (our choices are based on past experiences and future expectations). If you could capture all the conditions of a coin flip and repeat the conditions, the coin would always land on the same side.

2.) There is a possibility that this "random" "randomly" selected the other numbers more. You could theoretically flip a coin 40 times and it always lands on heads. There is a difference between potential random in this coin flip example being 50% chance of either side, and actual random, in this case being 100% heads and 0% tails.

Hope this information helped you!

Jeremy Castanza
Jeremy Castanza
12,081 Points

Just wanted to add to what Samuel wrote... the nature of computer programs being random is actually quite interesting since most are seeded using your computer's clock.

What that really boils down to is the "randomness" is really being driven by you and what nanosecond happens to register on your computer's clock when the Math.random method is called. If you're looking for a fun way to try exploiting this, try changing the intervals between when you execute the Math.random() method and see if you can get "1" that way.