Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Simulating and Calculating Probabilities

00:00 Great, you’re nearly there. Now it’s time to put it all together. In this session, you’re going to simulate and calculate probabilities by implementing a program that for one, uses the random module to generate random numbers and simulates many coin tosses.

00:19 Then it’s going to calculate the ratio of heads to tails, and it will allow you to alter the behavior of the coin so that you get different ratios of heads or tails.

00:31 One of the key modules that you’ll be using and when you’ll be simulating random events in general is the random module. You can import random with the import keyword, and after that, you can use one of the functions in the random module, such as randint(). randint() takes two numbers, a starting one and an ending one.

00:51 Then it will choose a random integer, a whole number between 1 and 10. So every time you run randint(), it’s going to give you a different result. This time, it was 9.

01:02 If you ran it again, it might give you 2, or it might give you 6 or 3.

01:08 Armed with this handy function from the random module, you can define a coin_flip() function. This coin_flip() function randomly returns "heads" or "tails" by using the random.randint() function.

01:23 It passes 0 as the first lower bound to randint() and 1 as the upper bound, so you’re only going to get 0 and 1 whenever you run this. You’ve got your if keyword here, and it’s checking whether this expression, random.randint(0, 1) == 0, whether that evaluates to True or False. If it evaluates to True, then it’s going to return "heads". If it evaluates to False, then it’s going to return "tails".

01:55 So you can roughly expect a fifty-fifty split between these two results whenever you call this function. With that function loaded up in IDLE, try and call it within your program …

02:12 and save and run it with F5. Oops, so you need to print the actual output of that because the function only returns. It doesn’t print. So now you’ve got heads.

02:26 Run it again. heads. Run it again. tails tails tails. A lot of tails in a row. heads heads. As you can see, it behaves roughly the same as a coin.

02:41 Let’s try and run this ten times. How might you run this ten times in the same program without having to type out coin_flip() ten times? You do that with a for loop.

02:51 So let’s go for i in r for … range(), good. Now, for i in range(10): print(coin_flip()).

03:06 Now you can run this with F5, and as you can see, you can get this to run ten times in quick succession. And as you can see, it does indeed behave a lot like a coin.

03:20 So let’s take that a step further and actually simulate this properly. Here you can initialize two variables, a heads_tally and a tails_tally, and basically these will serve to count the number of times that heads turns up or tails turns up.

03:36 Then you can create a huge for loop of 10_000 iterations, and you can execute the coin_flip() within an if clause and checking if it equals heads. And if it does, then you increment the heads_tally. And if it doesn’t, then you increment the tails_tally.

03:58 So go over to IDLE and type this out.

04:03 Okay. So you’ve got this typed up in IDLE. And remember just to do that below the coin_flip() function, because this depends on the coin_flip() function.

04:13 And then at the end, you can print("heads", heads_tally) and ("tails", tails_tally). You’re printing the string "heads" just so you know what you’re looking at. And over to the left here, you can see one result of the execution is that heads was slightly more common than tails. So how about you run this a couple times?

04:30 heads more common again. This time, tails is more common. But as you can see, there’s not much difference there.

04:41 How about you increase this to, say, 100_000_000 times? This might take a while.

04:56 That did take a while. It took a few minutes. But as you can see, the numbers are very close. So this is a pretty good coin.

05:07 This function is slightly different. It doesn’t use the randint() function. It uses the random() function from the random module.

05:15 The random() function is called without arguments, and it will return a floating-point number—that, is a decimal number between 0 and 1.

05:25 So it could return 0.535444443, something like in that order, anywhere between 0 and 1. So the idea with this function, which is called unfair_coin_flip(), is that you pass in a probability_of_tails.

05:39 So if you want it to be a fair coin tos, you can pass in a probability_of_tails of 0.5, and then this will roughly behave as a normal coin.

05:48 But if you pass in a probability_of_tails of 0.3, then you’re much more likely to get a heads as a result. So go and type this up in IDLE.

06:02 Here, you have your new function, unfair_coin_flip(). So let’s simulate that. Let’s not do a hundred million because that takes a long, long time.

06:13 Let’s do a hundred thousand because that’s quite a reasonable number for your computer to do very quickly. So instead of coin_flip() here, you’ll want to do unfair_coin_flip(),

06:28 and you’ll want a probability of 0.2, say. Okay, so now save this and run this. And as you can see, the probability is nearly 0.2. tails is almost at twenty thousand, while heads takes eighty thousand.

06:46 So now you can change this to something like 0.4, and let’s see if that works out properly. And yes, forty-sixty split, as might be expected.

07:01 Well done. You’ve simulated and calculated probabilities. In this session, you implemented a program that used the random module and two of its functions.

07:10 And you simulated many, many, many coin tosses. You calculated the ratio of heads to tails in these simulations. And you wrote another function that allowed you to alter the behavior of the coin, leaning heavily towards tails or heads, depending on what you pass into that function.

Become a Member to join the conversation.