Probability of getting all possible values on throwing N dices

Given an integer N denoting the number of dices, the task is to find the probability of every possible value that can be obtained by throwing N dices together.

Examples:

Input: N = 1
Output:
1: 0.17
2: 0.17
3: 0.17
4: 0.17
5: 0.17
6: 0.17
Explanation: On throwing a dice, the probability of all values from [1, 6] to appear at the top is 1/6 = 0.17

Input: N = 2
Output:
2: 0.028
3: 0.056
4: 0.083
5: 0.11
6: 0.14
7: 0.17
8: 0.14
9: 0.11
10: 0.083
11: 0.056
12: 0.028
Explanation: The possible values of the sum of the two numbers that appear at the top on throwing two dices together ranges between [2, 12].

Approach: The idea is to use Dynamic programming and DP table to store the probability of each possible value.



  • Store the probabilies of all the 6 numbers that can appear on throwing 1 dices.
  • Now, for N=2, the probability for all possible sums between [2, 12] is equal to the sum of product of the respective probability of the two numbers that add up to that sum. For example,

    Probability of 4 on throwing 2 dices = (Probability of 1 ) * ( Probability of 3) + (Probability of 2) * ( Probability of 2) + (Probability of 3 ) * ( Probability of 1)

  • Hence for N dices,

    Probability of Sum S = (Probability of 1) * (Probability of S – 1 using N -1 dices) + (Probability of 2) * (Probability of S – 2 using N-1 dices) + ….. + (Probability of 6) * (Probability of S – 6 using N -1 dices)

  • Hence, in order to solve the problem, we need to fill dp[][] table from 2 to N using top-down approach using the relation:

    dp[i][x] = dp[1][y] + dp[i-1][z] where x = y + z and i denotes the number of dices

  • Display all the probabilities stored for N as the answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to calculate
// the probabilty of
// all the possible values
// that can be obtained
// throwing N dices
  
#include <bits/stdc++.h>
using namespace std;
  
void dicesSum(int n)
{
    // Store the probablities
    vector<map<int, double> > dp(n + 1);
    // Precompute the probabilities
    // for values possible using 1 dice
    dp[1] = { { 1, 1 / 6.0 },
              { 2, 1 / 6.0 },
              { 3, 1 / 6.0 },
              { 4, 1 / 6.0 },
              { 5, 1 / 6.0 },
              { 6, 1 / 6.0 } };
  
    // Compute the probabilies
    // for all values from 2 to N
    for (int i = 2; i <= n; i++) {
        for (auto a1 : dp[i - 1]) {
            for (auto a2 : dp[1]) {
                dp[i][a1.first + a2.first]
                    += a1.second * a2.second;
            }
        }
    }
    // Print the result
    for (auto a : dp[n]) {
        cout << a.first << " "
             << setprecision(2)
             << a.second
             << endl;
    }
}
  
// Driver code
int main()
{
    int n = 2;
    dicesSum(n);
  
    return 0;
}

chevron_right


Output:

2 0.028
3 0.056
4 0.083
5 0.11
6 0.14
7 0.17
8 0.14
9 0.11
10 0.083
11 0.056
12 0.028

Time Complexity: O(N2)
Auxilary Space: O(N2)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.