in reply to Probability sum of random variables

Using the Set::CrossProduct module, here is another approach to your present solution.
#!/usr/bin/perl use strict; use warnings; use Set::CrossProduct; use List::Util 'sum'; my $targetSum = 4; my @freq = qw (0.1 0.2 0.7); my $nElements = 2; my @combo = ([0..$#freq]) x $nElements; my $cross = Set::CrossProduct->new( \@combo ); my @ways; while( my $array_ref = $cross->get ) { push @ways, $array_ref if sum(@$array_ref) == $targetSum; } #Now calculate P-value my $pVal; for my $aref (@ways) { my $p = 1; for my $operand (@$aref) { $p *= $freq[$operand]; } $pVal += $p; } print "P-value(sum of $nElements = $targetSum) = $pVal\n";