An algorithm to calculate probability of a sum of the results happening
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In probability theory, determining the likelihood of obtaining a certain sum from a set of random, discrete events is a common problem. One classic example is determining the probability of achieving a particular sum when rolling multiple dice. This article provides a detailed explanation of an algorithmic approach to calculate the probability of a specified sum occurring when dealing with discrete random variables.
Probability Basics
Before diving into the algorithm, it's essential to understand some foundational concepts:
• Random Variable: A variable that can take on multiple values, each with an associated probability. • Probability Distribution Function (PDF): Describes the probability of the random variable taking on each possible value. • Discrete vs. Continuous Random Variables: Discrete variables take on distinct, separate values (like a die roll), while continuous variables cover a range (like measuring temperature).
For this article, we will focus on discrete random variables. Consider a simple example – a fair six-sided die has an equal probability of rolling any integer from 1 to 6.
Problem Statement
Given independent random variables, each representing a fair six-sided die, what is the probability of the sum being equal to a particular number ?
Combinatorial Approach
- Defining the Problem: Mathematically, we express this as finding , where is the outcome of die .
- Uniform Dice Roll: Each die has the same probability distribution:
- Total Outcomes: For dice, the total number of outcomes is .
- Counting Favorable Outcomes: To determine the number of ways to obtain a sum , we need to count the combinations of dice rolls that sum to .
Generating Function Approach
A more sophisticated method uses generating functions:
- Generating Function of a Single Die: Each die's generating function is given by:
- Generating Function of Dice: The generating function for dice is:
- Extracting Coefficient: The probability we seek is derived from extracting the coefficient of in and dividing by the total number of outcomes .
Dynamic Programming Solution
Another efficient way is using a dynamic programming table to store interim results and calculate the desired probabilities iteratively.
- DP Table Setup: Let represent the number of ways to reach sum using dice.
- Base Case: This implies that there's one way to reach a sum of 0 with 0 dice – doing nothing.
- Transition: For each die and sum: if .
- Final Calculation: The probability of sum is then:
Example Calculation
Consider calculating the probability of a sum of 7 with two dice ().
- DP Initialization: •
- Two Dice Calculation:
| Sum () | Ways with 1 Die | Ways with 2 Dice | |||
| 1 | 1 | 0 | |||
| 2 | 1 | 1 | |||
| 3 | 1 | 2 | |||
| 4 | 1 | 3 | |||
| 5 | 1 | 4 | |||
| 6 | 1 | 5 | |||
| 7 | 0 | 6 | 3. Probability: The number of ways to achieve a 7 with two dice is 6. The total number of outcomes with two dice is . Hence the probability: ## Key Points Summary | Concept | Description |
| --- | --- | --- | --- | --- | --- |
| Random Variable | A variable representing an event outcome with a probability distribution | ||||
| Probability distribution function for a random variable | |||||
| Combinatorial Approach | Counts all favorable outcomes versus total outcomes | ||||
| Generating Function | Uses algebraic functions to encode and manipulate sequences of probabilities | ||||
| Dynamic Programming | Builds a solution incrementally by storing outcomes of subproblems and avoiding recomputation |
The above methods provide a comprehensive framework for calculating the probability of specific outcomes when dealing with sums of discrete random variables. Each method lends itself to different scenarios based on computational efficiency and problem complexity.

