The Monte Hall Problem (http://en.wikipedia.org/wiki/Monty_Hall_problem) is a probability puzzle that says:
"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?"
Let's solve the Monte Hall Problem using a Monte Carlo simulation. We'll write a PHP program that simulates thousands of games and random choices. It will simulate you choosing a door randomly, Monte Hall opening one of the remaining doors (one without a car), and the result if you don't switch doors and if you switch doors.
Before you run the program, should you switch to the other door or stick with your choice?
<?php
// Using a Monte Carlo Simulation to solve the Monte Hall Problem by My Tiny Lab LLC (http://mytinylab.com). 2011/02/20
$car = 1;$youDidntSwitchDoors_wins = 0; // how many times you win the car if you don't switch
$youSwitchedDoors_wins = 0; // how many times you win the car if you switch doors$iterations = 100000; // # of iterations to solve the Monte Hall Problem
for ( $iters=0; $iters < $iterations; $iters++) {
// Randomly assign a car (value 1) and two goats (values 1 and 2) to three doors
$door[1] = rand(1,3);
// $door2 can't be the same value as $door1
do { $door[2] = rand(1,3);}while ($door[2] == $door[1]);
// $door3 can't be the same value as $door1 and $door2
do {$door[3] = rand(1,3);} while (($door[3] == $door[1]) || ($door[3] == $...




