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 trialNoah Fields
13,985 PointsIs 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
231,271 PointsThe 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:
- 166451
- 167288
- 165958
- 167469
- .166599
- 166235
While not totally even, it's pretty close and neither 1 nor 6 have the highest count.
Stephan Olsen
6,650 PointsIt'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/