#!/usr/bin/perl -w
use strict;
use v5.10;
use List::Util 'shuffle', 'reduce';
select((select(STDOUT), $| = 1)[0]);
my (@switch, @noswitch);
my @doors = ( 1, 0, 0 );;
my $m = 1;
for (my $i = 1; $i <= 10000000; $i++) {
shuffle(@doors);
my $choice = int(rand(3));
push @noswitch, $doors[$choice];
push @switch, !$doors[$choice];
if (0 == $i % $m) {
say "guess #" . $i;
say "doors: " . join ',', @doors;
say "choice: " . $choice;
say $doors[$choice] ? "won!" : "lost!";
say "switch win average: " . (reduce { $a + $b } @switch) * 100 / $i;
say "noswitch win average: " . (reduce { $a + $b } @noswitch) * 100 / $i;
$m *= 2;
say "next output at guess #" . $m;
}
}