aher

Chainmail joint probability of 6 LF v 6 LF scoring m,n kills

Mar 5th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.79 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # Chainmail joint probability of 6 LF vs 6 LF scoring (m,n) kills
  3. # using simultaneous turn sequence
  4.  
  5. sub nFactorial { # Factorial n!
  6.     my $n=shift;
  7.     my $product = 1;
  8.     while($n>0) {
  9.         $product *= $n--;
  10.     }
  11.     $product;
  12. }
  13.  
  14. sub nCr { # Combinations of n out of r
  15.     my ($n,$r) = @_;
  16.     int nFactorial($n) / int nFactorial($r) * int nFactorial($n-$r);
  17. }
  18.  
  19. # Probability of 6 LF scoring n kills against 6 LF, n in 0..6
  20. sub prob {
  21.     my ($n) = @_;
  22.     nCr(6,$n) * (1/6)**$n * (5/6)**(6-$n);
  23. }
  24.  
  25. for $c (0..6) { print " " x 10, $c; }; print "\n";
  26.  
  27. for $dA (0..6) {
  28.   print $dA, " ";
  29.   for $dB (0..6) {
  30.     my $p = prob($dA) * prob($dB);
  31.     printf " %10.5g", $p;
  32.     $check += $p;
  33.   }
  34.   print "\n";
  35. }
  36.  
  37. print "Check: sum of probabilities = ", $check, "\n"; # Must be unity
Advertisement
Add Comment
Please, Sign In to add comment