Probability of getting all possible values on throwing N dices | C ++ Program to calculate the probability of all the possible values that can be obtained throwing N dices ; Store the probabilities ; Precompute the probabilities for values possible using 1 dice ; Compute the probabilies for all values from 2 to N ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dicesSum ( int n ) { vector < map < int , double > > dp ( n + 1 ) ; 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 } } ; 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 ; } } } for ( auto a : dp [ n ] ) { cout << a . first << " ▁ " << setprecision ( 2 ) << a . second << endl ; } } int main ( ) { int n = 2 ; dicesSum ( n ) ; return 0 ; }