Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created May 3, 2017 19:12
Show Gist options
  • Save Caaz/64df0cbcca4d14325e814f1e28e513ee to your computer and use it in GitHub Desktop.
Save Caaz/64df0cbcca4d14325e814f1e28e513ee to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
my $rounds = 1000;
my $wins;
my @initialDoors = (0..100);
for(my $round = 0; $round < $rounds; $round++) {
my @doors = @initialDoors;
for my $door (@doors) { $door = 0; }
my $choice = int rand @doors;
my $correct = int rand @doors;
$doors[$correct] = 1;
for(my $i = 0; $i < @doors - 2; $i++) {
my $wrong;
do { $wrong = int rand @doors; }
while($wrong == $choice || $wrong == $correct || $doors[$wrong] == 2);
$doors[$wrong] = 2;
}
my $diff;
do { $diff = int rand @doors; }
while($diff == $choice|| $doors[$diff] == 2);
$wins++ if($diff == $correct);
}
print("Won $wins out of $rounds rounds. With ".(@initialDoors + 0)." doors.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment