#!/usr/bin/perl -- # Simulate the "Monty hall" gameshow problem: # Suppose you're on a game show, and you're given # the choice of three doors: Behind one door is a # car; behind the others, goats. You pick a door, # say No. 1, and the host, who knows what's behind # the doors, opens another door, say No. 3, which # has a goat. He then says to you, "Do you want to # pick door No. 2?" Is it to your advantage to # switch your choice? $swapwins=0; $noswapwins=0; $totaltries=1000; for ($i=0; $i<$totaltries;$i++) { # The price is behind door X. $prizedoor=int(rand(3))+1; # The door the contestant chooses is random. $initialchoice=int(rand(3))+1; # The door opened by the gameshow host is not # the initial choice, AND is not the prize door. do { $openedbygameshowhostdoor=int(rand(3))+1; } until $openedbygameshowhostdoor!=$initialchoice and $openedbygameshowhostdoor!=$prizedoor; # Now let's find out which door the contestant # would open if he makes the remaining choice for ($choice=1;$choice<=3;$choice++) { if (($choice!=$openedbygameshowhostdoor) && ($choice!=$initialchoice)) { $swapdoor=$choice; } } if ($initialchoice==$prizedoor) { $noswapwins++; } else { $noswaplosses++; } if ($swapdoor==$prizedoor) { $swapwins++; } else { $swaplosses++; } } print "Tried playing the game $totaltries times.\n"; print "Swapping rendered $swapwins wins and $swaplosses losses.\n"; print "Not swapping rendered $noswapwins wins and $noswaplosses losses.\n";