Advertisement
wfaulk

8d6-match.pl

Nov 29th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.19 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. my $iterations = 4200000;
  7.  
  8. sub rolldie {
  9.     my ($sides) = @_;
  10.     return (int(rand($sides)) + 1);
  11. }
  12.  
  13. sub rolldice {
  14.     my ($dice, $sides) = @_;
  15.     my @roll;
  16.     for (my $i=0; $i<$dice; $i++) {
  17.         $roll[$i] = rolldie($sides);
  18.     }
  19.     return \@roll;
  20. }
  21.  
  22. sub matchsets {
  23.     my ($setone, $settwo) = @_;
  24.  
  25.     my $string = printsets($setone, $settwo);
  26.  
  27.     my $match = 0;
  28.     foreach my $die (values(@$setone)) {
  29.         my $found = 0;
  30.         for (my $i=0; $i<scalar(@$settwo); $i++) {
  31.             if ($die == $$settwo[$i]) {
  32.                 #splice(@$settwo, $i, 1);
  33.                 $$settwo[$i]=0;
  34.                 $found = 1;
  35.                 last;
  36.             }
  37.         }
  38.         $match++ if $found;
  39.     }
  40.     if ($match == scalar(@$setone)) {
  41.         print("$string\n");
  42.         return(1);
  43.     } else {
  44.         return(0);
  45.     }
  46. }
  47.  
  48. sub printsets {
  49.     my ($setone, $settwo) = @_;
  50.  
  51.     my $string;
  52.  
  53.     foreach my $die (values(@$setone)) {
  54.         $string .= "$die ";
  55.     }
  56.     $string .= "/";
  57.     foreach my $die (values(@$settwo)) {
  58.         $string .= " $die";
  59.     }
  60.  
  61.     return($string);
  62. }
  63.  
  64. my $match=0;
  65. for (my $i=0; $i<$iterations; $i++) {
  66.     my $rollone = rolldice(8, 6);
  67.     my $rolltwo = rolldice(8, 6);
  68.     if (matchsets($rollone, $rolltwo)) {
  69.         $match++;
  70.     }
  71. }
  72.  
  73. print("$match in $iterations\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement