Moving on from tossing a single coin in the last lesson, let's now toss two coins. What we'll do here is to see how many times heads comes up on both coins (HH) versus something different (like HT, TH, or TT).
We'll start by streamlining the code by making a function called toss() which handles all of the tossing mechanics of a coin, just returning a 'h' for heads or 't' for tails.
Now you try. Finish the code, in particular the N= line and the line that looks to see if the toss() function returns an 'h' both times.
Type your code here:
See your results here:
Run the code with N=1000 a few times. What do you see? The chance of getting two heads should be about 0.25 or 25%. Why is this? Two ways of thinking about it:
All possible outcomes are HH, HT, TH, or TT. This is 4 possible outcomes. HH is only 1 of the 4, so the chance of getting HH is $1/4=0.25$ or 25%.
The chance of one coin showing a H is 0.5 or 50%. For BOTH coins to show H, we'd say "coin 1 shows H AND coin 2 shows H." AND in probability always means multiply. So, $0.5\times 0.5=0.25$.
Notice again in order for probability to come out right, we always requires a large number of trials.
Note: Is the outcome for TT, HT, or TH any different? Try it! (Ans: No, but why?)
Share your code
Show a friend, family member, or teacher what you've done!