Probability of getting all possible values on throwing N dices | Java 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
import java . io . * ; import java . util . * ; class GFG { static void dicesSum ( int n ) { double [ ] [ ] dp = new double [ n + 1 ] [ 6 * n + 1 ] ; for ( int i = 1 ; i <= 6 ; i ++ ) dp [ 1 ] [ i ] = 1 / 6.0 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = i - 1 ; j <= 6 * ( i - 1 ) ; j ++ ) for ( int k = 1 ; k <= 6 ; k ++ ) { dp [ i ] [ j + k ] += ( dp [ i - 1 ] [ j ] * dp [ 1 ] [ k ] ) ; } for ( int i = n ; i <= 6 * n ; i ++ ) { System . out . println ( i + " ▁ " + Math . round ( dp [ n ] [ i ] * 1000.0 ) / 1000.0 ) ; } } public static void main ( String [ ] args ) { int n = 2 ; dicesSum ( n ) ; } }