Session 4: Probability - Module C: Fair Dice Program

1. Overview

Discussion: How can we tell if a dice is really fair?
Examine the dice program code and its output.
Plot the results of the dice program.

2. Setting Up

Download fairDice.py:

  1. Download the python program fairDice.py into your programs directory.
  2. Open fairDice.py in gedit to view and edit the program.

Start IPython:

  1. Open a terminal window.
  2. Change the directory to your programs directory with the cd command.
  3. Start IPython with the ipython command.
  4. From IPython, you can run your program by typing run fairDice.py.

3. Run the Dice Program

The fairDice program randomly picks a number from 1 to 6 and writes it to a log file, but how do we know if the program is picking each number with equal probability?

Start by running the program: examine the output produced by the program in the file rolls.log. Can you tell just by looking at the output whether the probability of rolling each number is fair? What would be a better way to visualize the data instead of looking at the text output?

Find the line in the program that contains:

counter[rollResult] = counter[rollResult] + 1

What is being counted? How can the counter be used to help us visualize the data?

4. Plot the Data as a Histogram

Change the histogram variable from False to True.
After rerunning the program, can you tell if the die is fair? How can you tell?

What would happen if the number of rolls is increased? Try increasing the total number of rolls and see if your guess was correct (hint: the die was rolled 500 times).

Don't forget to save the histogram and answer these questions on your webpage!