Created
July 30, 2011 23:23
-
-
Save HansF/1116142 to your computer and use it in GitHub Desktop.
Simulation for Monty Hall Dilemma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
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 number 1, and the host, who knows what's behind the | |
doors, opens another door, say number 3, which has a goat. | |
He says to you, "Do you want to pick door number 2?" | |
Is it to your advantage to switch your choice of doors? | |
Craig. F. Whitaker | |
Columbia, MD | |
*/ | |
for ($i = 0; $i <1000000 ; $i++ ){ | |
// make choice | |
$keuze = mt_rand ( 1 , 3 ); | |
$prijs = mt_rand ( 1 , 3 ); | |
// goats | |
if ($prijs == 1) $goat1 = 2; | |
if ($prijs == 1) $goat2 = 3; | |
if ($prijs == 2) $goat1 = 1; | |
if ($prijs == 2) $goat2 = 3; | |
if ($prijs == 3) $goat1 = 1; | |
if ($prijs == 3) $goat2 = 2; | |
// keuze | |
if ($keuze == 1) $alt1 = 2; | |
if ($keuze == 1) $alt2 = 3; | |
if ($keuze == 2) $alt1 = 1; | |
if ($keuze == 2) $alt2 = 3; | |
if ($keuze == 3) $alt1 = 1; | |
if ($keuze == 3) $alt2 = 2; | |
//lets open some doors | |
if ($keuze == $goat1) $dooropen = $goat2; | |
if ($keuze == $goat2) $dooropen = $goat1; | |
if (($keuze != $goat1)&&($keuze != $goat2)){ | |
if (1==mt_rand ( 1 , 2 )){ | |
$dooropen = $goat2; | |
}else{ | |
$dooropen = $goat1; | |
} | |
} | |
// See if we would win by not swapping | |
if ($keuze == $prijs) $winnormaal++ ; | |
// Ok, let's take the other door... | |
if (($keuze==1)&&($dooropen==2)) $keuze = 3; | |
if (($keuze==1)&&($dooropen==3)) $keuze = 2; | |
if (($keuze==2)&&($dooropen==1)) $keuze = 3; | |
if (($keuze==2)&&($dooropen==3)) $keuze = 1; | |
if (($keuze==3)&&($dooropen==2)) $keuze = 1; | |
if (($keuze==3)&&($dooropen==1)) $keuze = 2; | |
// See if we would win by choosing alternative door | |
if ($keuze == $prijs) $winalt++ ; | |
} | |
echo "\nNormaal : $winnormaal \nAlt keuze: $winalt"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment