Flipping a Coin

Many introductory problems in statistics center around flipping coins. Given a fair coin, we know that the probability of getting a head is 0.5. How many heads can we expect to get when we flip the coin 10 times? 100 times? For many of these questions we can write a program, run experiments, and conclude an experimental probability.

Experimental probability is defined as the ratio of the number of times an event occurs to the total number of times the activity is performed. Mathematical probability, on the other hand, has to do with the number of possible outcomes of an event. For example, consider the probability of flipping a coin and getting heads. There are two possible outcomes: heads and tails. There is one favorable outcome: heads. The mathematical probability of flipping heads, then is ½. In these two examples, we will mostly express the mathematical probability in terms of the expected value. For example, if you flip a coin 10 times, how many times do you expect it to come up heads? This can be calculated by multiplying the number of flips (10) by the probability of getting heads on one flip (½), yielding an expected value of 5. The experimental probability depends upon the actual outcome of the experiment. Flip a coin. If it is heads, then the experimental probability is 1/1. If it is tails, it is 0/1. This shows that experimental probability is much more accurate for larger samples (i.e. flipping a coin 100 times vs. 1 time).

To determine the experimental probability, we could run an experiment in which we flip the coin 10 times and record the number of heads we get. But experiments work better with larger sample sizes (e.g. larger number of flips). To get a more accurate result, we might want to flip the coin 100 times or 1,000 times or 10,000,000 times. Such large experiments are no longer feasible to be done by hand. Using a random number generator,  a simulation allows the computer to “flip” the coin and a program records the results. Let’s start with the following questions:

  1. If we flip a fair coin 10 times, how many heads do we expect to get? 100 times? 100,000 times?
  2. If the coin is weighted so that it has a 2/3 chance of coming up heads, how many heads do we expect to get in 30 flips? 100 flips?  3,000 flips?

Programming Guidelines

To write a program determining the experimental probabilities of the questions stated above, we will simulate a coin toss. Before you start coding, decide how to handle the following:

  • How will you model the flip? What determines if it is heads or tails?
  • How will you keep track the results?
  • How can you model a weighted coin?
  • How do you make the number of flips variable so that can reuse the program for any number of flips?


To answer question 1, write a program modeling a coin toss.

  • Make the number of flips a variable.
  • Use a random number generator to simulate a coin toss.
  • Keep track of the number of heads and number of tails.

Run the program with different numbers of flips and compare the results to the expected values, which you should calculate ahead of time using basic probability principles. What do you notice about the accuracy of the results as the number of flips gets bigger?

Adjust your program to model a weighted coin and run the simulations again. A simple way to do this is to change the way the coin toss is simulated. For a regular coin, we randomly chose one of two numbers (1 or 2) and assigned heads and tails accordingly. (i.e. the two possible outcomes are 1 and 2 and one possible outcome represents heads, so the probability of getting heads is ½). If we want the probability of getting heads to be ⅔ instead, we just change the possible outcomes. Choose a random number from 1 to 3 and let two of the numbers represent heads. How accurate are the results compared to the expected value?

Sample Python Program

#Simulation of Coin Toss

import random
#simulations typically make use of random numbers 

n = 1000 #this value can easily be changed to change the sample size
heads = 0 
tails = 0

for i in range(n):
    flip = random.randint(1,2) # get a random number between 1 and 2
    if flip == 1: # head
        heads = heads + 1
    else:         # tail
        tails = tails + 1

print 'Heads:', heads
print 'Tails:', tails

#Extension: For a 'weighted' coin, the change is simple. Using the
#random numbers works because we assume that each number in the range
#has equal probability of being chosen, so in the range (1, 2), we have
#two equal possibilities, which model the two equal possibilities for the
#coin toss.
#Now consider a coin that has 2/3 chance of getting H and 1/3 chance of
#getting T. We can model this by choosing a random number from (1, 3) and
#letting two of the numbers represent H and 1 represent T. 

Internet resources