Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use warnings;
- use strict;
- my $iterations = 4200000;
- sub rolldie {
- my ($sides) = @_;
- return (int(rand($sides)) + 1);
- }
- sub rolldice {
- my ($dice, $sides) = @_;
- my @roll;
- for (my $i=0; $i<$dice; $i++) {
- $roll[$i] = rolldie($sides);
- }
- return \@roll;
- }
- sub matchsets {
- my ($setone, $settwo) = @_;
- my $string = printsets($setone, $settwo);
- my $match = 0;
- foreach my $die (values(@$setone)) {
- my $found = 0;
- for (my $i=0; $i<scalar(@$settwo); $i++) {
- if ($die == $$settwo[$i]) {
- #splice(@$settwo, $i, 1);
- $$settwo[$i]=0;
- $found = 1;
- last;
- }
- }
- $match++ if $found;
- }
- if ($match == scalar(@$setone)) {
- print("$string\n");
- return(1);
- } else {
- return(0);
- }
- }
- sub printsets {
- my ($setone, $settwo) = @_;
- my $string;
- foreach my $die (values(@$setone)) {
- $string .= "$die ";
- }
- $string .= "/";
- foreach my $die (values(@$settwo)) {
- $string .= " $die";
- }
- return($string);
- }
- my $match=0;
- for (my $i=0; $i<$iterations; $i++) {
- my $rollone = rolldice(8, 6);
- my $rolltwo = rolldice(8, 6);
- if (matchsets($rollone, $rolltwo)) {
- $match++;
- }
- }
- print("$match in $iterations\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement