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
Charles Franklin
17,535 PointsRandom Numbers... Or are they...
So I have my random number generator running fine. It passes muster.. But when I limit the random numbers to say 3, like this (by removing the -1 at the end)
I seem to get a large number of 2's and 3's. When I keep the -1 at the end like this
I seem to get a large number of 1's and 2's... Am I seeing this correctly or am I looking at it with a biased eye... Hope my code works, gonna play with this some more to see if I can resolve it..
chip
2 Answers
Jason Anders
Treehouse Moderator 145,863 PointsHey Charles,
The way Math.random works, if you remove the +1 and use Math.floor, you shouldn't get any 3s at all. When you put in the values 1 and 3, the program will generate a float from and including 1 up to but not including 3. So, if the number generated is 2.999538576, it will still round down to 2. This is why you add the +1 and use Math.floor.
With the +1, however, it is statistically reasonable that you will receive more 2s and 3s than ones. There is a ~66% chance of a 2 or a 3, but only ~33% chance of a 1.
So, it would be normal you are seeing the results you are seeing when only using a range of 1 to 3.
Hope this helps. :)
Jason
Jacob Mishkin
23,118 PointsIf you would like to know more about math.random and its relationship to browsers check out this blog:
Jason Anders
Treehouse Moderator 145,863 PointsInteresting read... Thanks Jacob. :)
Jacob Mishkin
23,118 PointsNot a problem. Glad you liked it!