The post Understand probabilities of unrelated events (and escaping zombies) explains how to calculate the number of trials necessary to reach a certain probability of an event occurring. It explains that 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}.

Suppose you want to know how many trials are necessary for this probability to equal some value Q so Q = 1 – (1 – P)^{N}. Solving for N gives N = Log_{(1 – P)}(1 – Q).

This example uses the following code to calculate this number of trials.

// See how many trials it takes to get to the target probability. private void btnGo_Click(object sender, EventArgs e) { lvwResults.Items.Clear(); // See if the event probability contains a % sign. bool percent = txtEventProb.Text.Contains("%"); // Get the event and target probabilities. double event_prob = ParseProbability(txtEventProb.Text); double target_prob = ParseProbability(txtTargetProb.Text); // Get the probability of the event not happening. double non_prob = 1 - event_prob; // target_prob = 1 - non_prob ^ power. Solving for power gives // power = Log(1 - target_prob) base non_prob. double calculated_trial = Math.Log(1 - target_prob, non_prob); // Display probaility for 1 less trial. double trial = (int)calculated_trial; double prob = 1.0 - Math.Pow(non_prob, trial); DisplayTrial(trial, prob, percent); // Display probaility for the calculated trial. trial = calculated_trial; prob = 1.0 - Math.Pow(non_prob, trial); DisplayTrial(trial, prob, percent); // Display probaility for 1 more trial. trial = (int)calculated_trial + 1; prob = 1.0 - Math.Pow(non_prob, trial); DisplayTrial(trial, prob, percent); }

The user enters the event probability and the desired target probability and clicks Go. The code determines whether the event probability contains a % sign to decide whether it should display values as percentages (50%) or decimal values (0.5). It then calls the `ParseProbability` method to read the event and target probabilities.

Next the code calculates 1 minus the event probability, 1 – P in the equations shown above. It uses the equation N = Log_{(1 – P)}(1 – Q) to calculate the number of trials necessary to reach the target probability. Finally the program uses the `DisplayTrial` method to display the probability for the calculated number of trials plus the probabilities of the next smaller and larger integer number of trials. For example, if the calculated result is 68.9 trials, the program displays the probability for that number of trials plus those for 68 and 69 trials.

The following code shows the `ParseProbability` method that reads the probability values entered by the user.

// Parse a probability that may include a percent sign. private double ParseProbability(string txt) { // See if the probability contains a % sign. bool percent = txt.Contains("%"); // Get the probability. double prob = double.Parse(txt.Replace("%", "")); // If we're using percents, divide by 100. if (percent) prob /= 100.0; return prob; }

This method determines whether the text includes a % sign and converts the text into a number. If the text includes the % sign, the method divides the result by 100 to return a decimal value (for example, 0.25 instead of 25%).

The following code shows the `DisplayTrial` method that displays a trials number and a probability in the form’s `ListView` control.

// Display a trial and its probability. private void DisplayTrial(double trial, double prob, bool percent) { ListViewItem new_item = lvwResults.Items.Add(trial.ToString()); if (percent) { prob *= 100.0; new_item.SubItems.Add(prob.ToString() + "%"); } else { new_item.SubItems.Add(prob.ToString()); } }

The code creates a new `ListViewItem` to display the number of trials. It then creates a subitem to show the corresponding probability. If the boolean parameter `percent` is `true`, the code formats the probability as a percentage (30%) instead of as a decimal value (0.30).