Probability of getting all possible values on throwing N dices | JavaScript 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 ; Prlet the result ; Driver Code
function dicesSum ( n ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < 6 * n + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= 6 ; i ++ ) dp [ 1 ] [ i ] = 1 / 6.0 ; for ( let i = 2 ; i <= n ; i ++ ) for ( let j = i - 1 ; j <= 6 * ( i - 1 ) ; j ++ ) for ( let k = 1 ; k <= 6 ; k ++ ) { dp [ i ] [ j + k ] += ( dp [ i - 1 ] [ j ] * dp [ 1 ] [ k ] ) ; } for ( let i = n ; i <= 6 * n ; i ++ ) { document . write ( i + " " + Math . round ( dp [ n ] [ i ] * 1000.0 ) / 1000.0 + " " ) ; } } let n = 2 ; dicesSum ( n ) ;