Flipping Coins
Explore how to simulate coin flips using arrays and random variables in Python. Learn to count outcomes with efficient coding techniques, sum results across multiple flips, and visualize data with bar graphs. Understand how to compute and plot experimental and cumulative probabilities from your simulations.
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:
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, we could have used a condition for the indices combined with a summation, as follows:
In lines 6 and 7, we use == condition on the array flips ...