Running test 1 (no switch)
Running test 2 (switch)
Results (n = 10000):
+------------------------+------------------------+
| With switching | Without switching +
+-----------+------------+-----------+------------+
| win | lose | win | lose |
+-----------+------------+-----------+------------+
| 6686x | 3314x | 3380x | 6620x |
| 66.9% | 33.1% | 33.8% | 66.2% |
+-----------+------------+-----------+------------+
Here is how it is done. Below is the source code of this file (threedoors_brainteaser.php).
<?php
/**
* Script: Three Doors Brainteaser emperical test
* Author: Klaus Guenther <thesaur@php.net>
* Version: 1.0.2 (2005-01-17)
* URL: http://thesaur.org/scripts/threedoors_brainteaser.php
*
* Copyright 2005 Klaus Guenther. All rights reserved.
*
* This code may be freely copied for demonstration
* purposes provided this disclaimer and copyright
* notice are retained unaltered.
*
*/
// Begin plain text output.
echo "<pre>\n";
// Any integer up to 1,000,000 should work without
// messing up the output. Above that, the output
// table may no longer be aligned, but the results
// will be no less accurate. A low number may
// result in widely varying outcomes.
$n = 10000;
// This sets up arrays to hold the number of wins
// and losses for each.
$results1 = array('win'=>0,'lose'=>0);
$results2 = array('win'=>0,'lose'=>0);
echo 'Running test 1 (no switch)';
// Run the test the number of times contained in
// variable $n.
for ($i=0;$i<$n;$i++) {
// Randomly select the door that contains the prize.
$good = rand(1, 3);
// Randomly select the door for the initial choice.
$choice1 = rand(1, 3);
// if the first door is the prize door, you win.
if($good == $choice1) {
$results1['win']++;
} else {
$results1['lose']++;
}
/*
// Enable for commandline usage.
// It prints a "." for each percent finished.
if (0 == ($i % ($n / 100))) {
echo '.';
}
*/
}
echo "\n";
echo 'Running test 2 (switch)';
// Run the test the number of times contained in
// variable $n.
for ($i=0;$i<$n;$i++) {
// Randomly select the door that contains the prize.
$good = rand(1, 3);
// Randomly select the door for the initial choice.
$choice1 = rand(1, 3);
// if the first door isn't the prize door, you win.
if($good != $choice1) {
$results2['win']++;
} else {
$results2['lose']++;
}
/*
// Enable for commandline usage.
// It prints a "." for each percent finished.
if (0 == ($i % ($n / 100))) {
echo '.';
}
*/
}
echo "\n";
// Format the output strings for the number
// of wins/losses.
$win1 = str_pad($results1['win'], 6, ' ', STR_PAD_LEFT).'x';
$lose1 = str_pad($results1['lose'], 6, ' ', STR_PAD_LEFT).'x';
$win2 = str_pad($results2['win'], 6, ' ', STR_PAD_LEFT).'x';
$lose2 = str_pad($results2['lose'], 6, ' ', STR_PAD_LEFT).'x';
// Generate and format the output strings for
// the percentage of wins/losses.
$pw_1 = str_pad(round((100 / $n) * $results1['win'], 1), 4, ' ', STR_PAD_LEFT).'%';
$pl_1 = str_pad(round((100 / $n) * $results1['lose'], 1), 4, ' ', STR_PAD_LEFT).'%';
$pw_2 = str_pad(round((100 / $n) * $results2['win'], 1), 4, ' ', STR_PAD_LEFT).'%';
$pl_2 = str_pad(round((100 / $n) * $results2['lose'], 1), 4, ' ', STR_PAD_LEFT).'%';
echo "\n";
// This line contains the number of times each test was run.
echo "Results (n = ".$n."):\n";
// Draw a text table to contain the results.
echo "+------------------------+------------------------+\n";
echo "| With switching | Without switching +\n";
echo "+-----------+------------+-----------+------------+\n";
echo "| win | lose | win | lose |\n";
echo "+-----------+------------+-----------+------------+\n";
echo "| ".$win2." | ".$lose2." | ".$win1." | ".$lose1." |\n";
echo "| ".$pw_2." | ".$pl_2." | ".$pw_1." | ".$pl_1." |\n";
echo "+-----------+------------+-----------+------------+\n";
// End the plain text output.
echo "\n</pre>\n";
// Draw a horizontal rule before dumping the source.
echo "<hr />\n";
// Add a comment identifying the source code
// (including the filename, for verification).
echo "Here is how it is done. Below is the source code of this file ("
. array_pop(explode(DIRECTORY_SEPARATOR, __FILE__)).").<br /><br />\n";
// Dump the source code.
highlight_file(__FILE__);