In this section, we'll look at some code and lessons that will help you to learn about the chance (or the probability) that something will happen. We'll use coin tosses and rolls of a dice since they're so familiar. Let's get started.
To begin, we'll simulate a coin toss here using the math.random function covered in a past lesson. We'll call math.random(1,100), and assume a "head" if the result is $<50$ and tails is the result is $\ge 50$.
What we'd like to do here is to see how many heads and tails we'd get when we toss a coin a few times, say 4 times. You probably already know the answer, but let's look anyway.
Now you try. Finish the code, in particular the N= line and the two lines that count the number of heads and tails we get.
Type your code here:
See your results here:
Run the code with N=4 a few times. What do you see? Do heads and tails always come up in the same amounts each time? Why not? What happens if you make N larger?
First lesson about probability here: "believable" probability always requires a large number of trials.
Share your code
Show a friend, family member, or teacher what you've done!