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) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Noah Fields
Noah Fields
13,985 Points

Is the random number generated biased towards 1 and 6?

I've noticed in my own code that one and six seem to occur more often than other results. This could very well be coincidence, or my own bias failing to realize that they are in fact occurring evenly, but I'm uncertain - is this random number generator as close to random as it could be (as I am aware that it is truly pseudorandom, not true random), or is there a slight bias towards extremes?

2 Answers

Steven Parker
Steven Parker
231,271 Points

The actual distribution depends on the implementation in your browser's JavaScript engine, but I ran a million samples in my Chrome browser and got these counts:

  1. 166451
  2. 167288
  3. 165958
  4. 167469
  5. .166599
  6. 166235

While not totally even, it's pretty close and neither 1 nor 6 have the highest count.

Stephan Olsen
Stephan Olsen
6,650 Points

It's just a coincidence that it seems biased, it really is random. I created a jsfiddle with the example Steven is talking about if you're curions to see. https://jsfiddle.net/v9rtatt0/1/