#! perl -slw use strict; use List::Util qw[ shuffle ]; our $TRIALS ||= 1_000_000; my $DIVISOR = $TRIALS / 100; my( $stick, $switch, $loseEitherWay ); for my $try ( 1 .. $TRIALS ) { ## Randomly hide the prizes behind the doors. my @doors = shuffle 'car', 'goat', 'goat'; ## Pick a door my $choice = int rand 3; ## The host opens a door that I didn't pick my $opened = ( grep{ $_ != $choice } 0 .. 2 ) [ rand 2 ]; ## If the host picks the car, I lose anyway. $loseEitherWay++, next if $doors[ $opened ] eq 'car'; ## Count my wins if I stick or switch $doors[ $choice ] eq 'car' ? $stick++ : $switch++; } printf "Odds of Losing either way: %.3f Win if you don't switch: %.3f Win if you do switch: %.3f\n", $loseEitherWay / $DIVISOR, $stick / $DIVISOR, $switch / $DIVISOR; __END__ P:\test>384288 Odds of Losing either way: 33.370 Win if you don't switch: 33.307 Win if you do switch: 33.324 P:\test>384288 -TRIALS=5000000 Odds of Losing either way: 33.318 Win if you don't switch: 33.336 Win if you do switch: 33.346