Flipping Coins
In this lesson, we will discuss a common problem with random variables: flipping coins.
We'll cover the following...
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 npimport matplotlib.pyplot as pltimport numpy.random as rndflips = rnd.randint(0, 1+1, 100) #generating random numbers between 0 and 1headcount = 0tailcount = 0for i in range(100):if flips[i] == 0:headcount += 1else:tailcount += 1print('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, ...