Code Sample: PHP

View source code

What is this? A few months ago, I encountered the Monty Hall Problem (or paradox): there are 3 doors, one with a car behind it and the other two with goats. (This scenario doesn't address those who would rather win a goat than a car.) After the player chooses a door, the host opens one of the other two doors, revealing a goat, and asks the player choose again. Should the player stick with the original choice or switch to the other door?

The answer: always switch. The odds of the car being behind the other door are twice as high (2/3 to 1/3).

That bent my brain a little, so I wrote a small PHP script to prove it. Now I'm using it here as a short code sample. Enter a number of simulations to run and it returns the number of wins and losses when the player switches doors. The results will be somewhere around 67% to 33%. Click "Verbose" to see details.

Future enhancements:

  • Ideally, this should run as a custom Drupal module rather than in an iframe.