Microsoft Visual C# Example: The Binomial Probability
The binomial probability is a measure of the likelihood (probability) of successfully getting a certain of occurrences out of a total number of trials…
2 min read497 words
Introduction#
Binomial probability measures the chance of achieving a specific number of successes over a fixed number of trials. Imagine you have a farm where testing shows a 0.75 probability of growing green peas. If you plant seeds that result in five offspring, you might want to determine the likelihood that exactly three of them are green. The formula for this binomial probability is:
The factors in this formula are:
| Factor | Description |
|---|---|
| n | The number of trials |
| x | The number of successful trials among n values. The values of x go from 0, 1, 2, ..., to n |
| p | The probability of success in a (any one) trial |
| q | The probability of failure in a (any one) trial. q is considered as q = 1 - p |
Hands-on Exercise: Building the Application
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New Project...
- In the middle list, click Windows Forms Application
- Change the Name to BinomialProbability1
- Click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Exercise.cs and press Enter
- Design the form as follows: Control (Name) Text TextAlign Other Properties Label &Number of Trials: TextBox txtTrials Right Label Number of &Successes: TextBox txtSuccesses Right Label &Probability of Success: TextBox txtProbabilitySuccess Right Button btnCalculate C&alculate PictureBox BackColor: Black Size -> Height: 2 Label &Binomial Probability: TextBox txtBinomialProbability Right Button btnClose Close
- Double-click the Calculate button on the form
- Change the file as follows: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace BinomialProbability1 { public partial class Exercise : Form { public Exercise() { InitializeComponent(); } private static long Factorial(long x) { if (x <= 1) return 1; else return x * Factorial(x - 1); } private static long Combination(long a, long b) { if (a <= 1) return 1; return Factorial(a) / (Factorial(b) * Factorial(a - b)); } private double BinomialProbability(int trials, int successes, double probabilityOfSuccess) { double probOfFailures = 1 - probabilityOfSuccess; double c = Combination(trials, successes); double px = Math.Pow(probabilityOfSuccess, successes); double qnx = Math.Pow(probOfFailures, trials - successes); return c * px * qnx; } private void btnCalculate_Click(object sender, EventArgs e) { int nbrOfTrials = 0, nbrOfSuccesses = 0; double probOfSuccesses = 0.00; double binomial = 0.00; try { nbrOfTrials = int.Parse(txtTrials.Text); } catch (FormatException) { MessageBox.Show("The number of trials is not valid", "Binomial Probability"); } try { nbrOfSuccesses = int.Parse(txtSuccesses.Text); } catch (FormatException) { MessageBox.Show("The number of successes is not valid", "Binomial Probability"); } try { probOfSuccesses = double.Parse(txtProbabilitySuccess.Text); binomial = BinomialProbability(nbrOfTrials, nbrOfSuccesses, probOfSuccesses); txtBinomialProbability.Text = binomial.ToString(); } catch (FormatException) { MessageBox.Show("The number of trials is not valid", "Binomial Probability"); } } } }
- Return to the form and double-click the Close button
- Implement the event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
- Return to the form
- To execute, press F5
- Enter some values and click Calculate
- Close the form and return to your programming environment
Home Copyright © 2010-2011 C# Key

