#!/usr/bin/perl -w

# program to model probability of picking prize (car) when there are
#    three curtains of which two have a goat (booby prize) behind.

# The contestant is allowed to guess one of the three curtains.

# The compare (who knows which prizes are behind which curtains) then
# opens one of the other sets of curtains but this is always one of the goats.

# The contestant then has the opportunity to change his selection or go with
# the other unopened curtain.

# Should the contestant swap or stick to improve his/her chances
# or does it make any difference at all?
$goat = 0;
$car = 1;

$n = 3000;
foreach $swap (0..1) {
  $win = 0;
  foreach (1..$n) {
    # assemble the stage
    @curtains[0..2] = ($goat, $goat, $goat);
    # choose which curtain to put the car behind
    $dice = int(rand(3));
    # replace the selected goat with the car
    $curtains[$dice] = $car;
    # contestant picks one.
    $pick = int(rand(3));
    # compare chooses one of the others - one with a goat.
    while (1) {
      $show = int(rand(3));
      if ($show == $pick) {next};
      if ($curtains[$show] == $car) {next};
      last;
    }
    # if we want to swap, do so.
    if ($swap) {$pick = 3 - $show - $pick}
    # Have we won?
    if ($curtains[$pick] == $car) {$win++;}
  }
  print "$win of $n  =  " . int($win / $n * 100 + 0.5) . "% for ";
  if ($swap) {print "swapped\n"} else {print "not swapped\n"}
}
