Robson » Code » PHP » Monty Hall Problem

 
<pre><?
 
/*
   I, the creator of this work, hereby release this into the
   public domain. This applies worldwide.
   In case this is not legally possible, I grant any entity
   the right to use this work for any purpose, without any
   conditions, unless such conditions are required by law.
       - Robson
*/
 
/*
 
   this script simulates the monty hall problem:
   http://en.wikipedia.org/wiki/Monty_Hall_problem
 
   1. it does one example with and without switching
   2. then it does a large number with and without
          switching and shows the results
 
*/
 
   // seed the random number generator
   srand();
   // output = whether to output information as the script runs
   // switch = whether to switch when the host shows a donkey
   function monty_hall_problem($output=1, $switch=1)
   {
       // there are two donkeys and one car
       $prizes = array('donkey', 'donkey', 'car');
       // shuffle the prizes so they appear in a random order
       shuffle($prizes);
       // print out the order
       if ($output)
           echo 'doors: ' . implode($prizes, ', ');
       // the player selects a random door
       $player = array_rand($prizes);
       // show the door the player picked and what is behind it
       if ($output)
           echo '<br/>Player chooses door ' . ($player+1) . ' (' . $prizes[$player] . ')';
       // now find a door with a donkey for the host
       do
       {
           // choose random door for the host
           $host = array_rand($prizes);
       // check if the door isn't the players and contains a donkey
       // if not, continue selecting random doors until a valid one is found
       } while ($host == $player || $prizes[$host] == 'car');
       // display the door chosen by the host
       if ($output)
           echo '<br/>Host choose door ' . ($host+1) . ' (' . $prizes[$host] . ')';
       // if the player wants to switch
       if ($switch)
           // find the other door
           for ($final = 0; $final == $player || $final == $host; $final++);
       else    
           // keep the same door
           $final = $player;
       // show the door chosen by the player
       if ($output)
           echo '<br/>Player chooses door ' . ($final+1) . ' (' . $prizes[$final] . ')';
       // return true if they won the car;
       return intval($prizes[$final] == 'car');
   }
   // output an example of each
   echo '<b>Example without switching:</b><br/>';
   echo '<br/>Result: ' . monty_hall_problem(1, 0);
   echo '<hr/><b>Example with switching:</b><br/>';
   echo '<br/>Result: ' . monty_hall_problem(1, 1);
   // the number of simulations to run
   $max = 50000;
   echo '<hr/>Running ' . number_format($max) . ' examples without switching: ';
   // loop through the number of specified simulations
   for ($n = 0; $n < $max; $n++)
       // add one if they win
       $success += monty_hall_problem(0, 0);
   // show how many have won
   echo round(($success / $max) * 100, 3) . '% won';
   // reset counter
   $success = 0;
   echo '<hr/>Running ' . number_format($max) . ' examples with switching: ';
   // loop through the number of specified simulations
   for ($n = 0; $n < $max; $n++)
       // add one if they win
       $success += monty_hall_problem(0, 1);
   // show how many have won
   echo round(($success / $max) * 100, 3) . '% won';
}
?></pre>
 
© 2004-17 robson | cc unless stated