Lesson goal: Checking random numbers

Previous: Divisibilty tester (2) | Home | Next: Graphing inequalities (x-axis)

As disussed in this lesson, math.random can be used to find a random number. Like cards in gambling, should we wonder if the computer's random number generator is "fair?" That is, are all numbers within the random range we select equally likely to appear?

The theory is this: if we choose random numbers between 1 and 10, and we do this 100 times. We'd generally expect each number (1,2,3,4,5,6,7,8,9 and 10) to come up 1/10th of the time, or we'd expect to see each number come up 10 times in the 100 choices. This sounds fair, right?

Let's test this theory here using a for-loop, if-statement, and the == operator. Let's see if math.random is "fair." If you ask for $N$ random numbers, and there are $m$ possible numbers, then you'd expect each number to come up $M/m$ times. So asking for $N=100$ random numbers between $1$ and $10$, each number should come up $100/10=10$ times. Confused? Think of a die. Roll it 100 times, you'd expect each number to come up $100/6=16.6$; we'd expect each number to appear about 16 times.

Now you try. After fixing the code, click the "run" button a few times and see if your target number comes up about total/10 times. The larger you make total, the better your answer should be.

Type your code here:


See your results here: