- Total number of wins where switching helped: 7, 70%
- Total number of wins where switching hurt: 3, 30%
<?php
$n = isset($_GET['n']) && preg_match("/^\d+$/", $_GET['n']) ? $_GET['n'] : "10";
// Be nice to my host -- let's keep the sample sizes relatively small.
if ($n > 10000) exit();
/*
the_full_monty() -- runs through iterations of a monty_hall scenario.
argument $switch determines whether it should always switch (true) or never switch (false)
*/
function the_full_monty($samplesize, $switch = true) {
}
define('CAR', 1);
define('GOAT', 0);
class MontyHallSim {
private $iterations = 10; // Total
private $no_switch_wins = 0; // Number of wins
private $switch_wins = 0;
private $doors = array(3);
private $selection = 0;
private $new_selection = -1;
private $revealed = 0;
public function __construct($iterations){
$this->iterations = $iterations;
}
public function run() {
$this->no_switch_wins = 0;
$this->switch_wins = 0;
for ($i = 0; $i < $this->iterations; $i++) {
$this->doors = array(GOAT, GOAT, GOAT); // Reset doors to all goats
$this->doors[rand(0,2)] = CAR; // Replace one of them with a car
$this->selection = rand(0,2); // Contestant chooses a door
// Reveal one of the goats
$this->revealed = -1;
do {
$this->revealed++;
} while ($this->doors[$this->revealed] == 1 || $this->revealed == $this->selection);
// If set to always-switch, then switch choices.
/* The bitwise operator works because:
Sel Rev Sum ~Sum
0 (00) 1 (01) 1 (01) 2 (10)
0 (00) 2 (10) 2 (10) 1 (01)
1 (01) 2 (10) 3 (11) 0 (00)
*/
$this->new_selection = ($this->selection + $this->revealed) ^ 3;
if ($this->doors[$this->new_selection] == CAR) {
$this->switch_wins++;
}
if ($this->doors[$this->selection] == CAR) $this->no_switch_wins++;
} // endfor
}
public function graph() {
}
public function stats() {
?> <div id="data"><ul> <?php
echo "<li>Total number of wins where switching helped: "
. $this->switch_wins . ", " . 100*($this->switch_wins / $this->iterations) . "%</li>";
echo "<li>Total number of wins where switching hurt: "
. $this->no_switch_wins . ", " . 100 * ($this->no_switch_wins / $this->iterations) . "%</li>";
?> </div> <?php
}
}
?>