Flipping Coins

In this lesson, we will discuss a common problem with random variables: flipping coins.

Flipping once #

Now that we have discussed random number generators, let’s look at the most common example used in probability: flipping a coin.

Let’s flip a coin 100 times and count the number of heads, denoted by 0, and the number of tails, denoted by 1:

Press + to interact
import numpy as np
import matplotlib.pyplot as plt
import numpy.random as rnd
flips = rnd.randint(0, 1+1, 100) #generating random numbers between 0 and 1
headcount = 0
tailcount = 0
for i in range(100):
if flips[i] == 0:
headcount += 1
else:
tailcount += 1
print('number of heads:', headcount)
print('number of tails:', tailcount)

First of all, note that the number of heads and tails adds up to 100. Also, note how we counted the heads and tails. We created counters headcount and tailcount, looped through all flips, and added 1 to the appropriate counter. Instead of a loop, ...