[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Understand the probability of unrelated events (and escaping zombies)

[Understand the probability of unrelated events (and escaping zombies)]

Given some event with a relatively low probability, what is the probability that the event will occur if you perform many trials? For example, suppose the zombie apocalypse has arrived and there's a 1% chance that the zombies will get you with every trip you make to the store for supplies. What's the probability that they get you if you go 10 times? 100 times? How many trips would you need to make before the probability of being caught is 50%?

For a more intuitive example, consider coin flips. At every flip, there's a 50% chance that you'll get heads. What is the probability that you'll get at least one heads in 2 flips? 5 flips?

To figure this out, you need to know a little bit about the probabilities of multiple events. If two events (such as two coin flips) are unrelated, then the probability of getting a specific result in one event AND another is the product of the probabilities of getting the events separately.

For example, what is the probability of getting heads in a coin flip and then getting heads again in a second flip? There's a 50% or 0.5 probability of getting heads on each flip, so the probability of getting heads and then heads again is 0.5 * 0.5 = 0.25.

This also makes sense if you consider all of the possible combinations of flips. You could get: HH, HT, TH, or TT. There are 4 possible outcomes and only 1 is the one you're looking for (HH) so the probability is 1/4 = 0.25.

Now consider a case more similar to the original zombie scenario. If you flip a coin twice, what is the probability that you get heads in the first flip OR in the second flip? In other words, in two coin flips what is the probability that at least 1 will be heads? Many people intuitively think that you can add the probabilities of the two separate events. In this case, that would be 0.5 + 0.5 = 1. Clearly that's not right because a probability of 1 means the result must occur, but clearly there is a chance that you will not get heads in two flips.

Looking at the possible outcomes (HH. HT, TH, TT) you can see that 3 of the 4 possible outcomes includes at least one heads, so the probability should be 3/4 = 0.75.

So how do you figure this out mathematically? The key is to look at the logical operator words AND and OR. Suppose you have two events E1 and E2 with probabilities P1 and P2. The probability of E1 and E2 is P1 * P2. Simple. Unfortunately you cannot simply add probabilities to find the probability of E1 or E2.

Fortunately there's a way around this. Logically E1 or E2 is equivalent to NOT ((NOT E1) AND (NOT E2)). For example, Heads OR Heads is equivalent to NOT (Tails AND Tails). (You may need to think about this for a while to convince yourself.)

We already know you can calculate the AND probability by multiplying two probabilities. Fortunately handling the NOTs is also easy. If an event has probability P, then the probability of the event not occurring is 1 - P.

To get back to the E1/E2 example, this means the probability of E1 OR E2 is 1 - ((1 - P1) * (1 - P2)).

For coin flips, this gives 1 - (0.5 * 0.5) = 1 - 0.25 = 0.75, which we saw earlier was correct.

You can use the same approach for greater numbers of events. For example, the probability of getting heads at least once in 3 coin flips is 1 - (0.5 * 0.5 * 0.5) = 1 - 0.875.

In general if an event has a probability P of occurring at each trial, then after N trials the probability that at least one trial resulted in the event is 1 - (1 - P)N.

Now we can finally get back to the zombie problem. If the probability of getting caught in 1 trip to the store is 0.01, then the probability of NOT getting caught is 0.99. That means the probability of not caught in two trips it's 1 - (probability of NOT getting caught)2 = 1 - 0.992 = 0.0199. In three trips it's 1 - 0.993 = 0.029701. In general the probability of being caught after N trips is 1 - 0.99N.

Trips% Caught
11.00%
21.99%
32.97%
43.94%
54.90%
65.85%
76.79%
87.73%
98.65%
109.56%

So the next question is, how many trips do you need to make before the probability of getting caught is 50%? The equation for the probability is 0.5 = 1 - 0.99N. Solving for N gives N = Log0.99(1 - 0.5) = 68.967563. Because you can't make fractional trips (well, I suppose it would be a fractional trip if you got caught halfway there), the probability of being caught is less than 50% after 68 trips and more than 50% after 69 trips.

In the next couple of days, I'll post programs to perform these calculations. If you want, you can try to write programs to calculate these on your own first.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.