Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The Monty Hall Paradox - Monte Carlo experiment (Monty Hall Problem Simulation)
<?php
function remove_door($doors, $number) {
unset($doors[$number]);
return $doors;
}
$primaryChoiceWon = 0;
$secondChoiceWon = 0;
$doorsCount = 3;
$tries = 1;
do {
$doors = array_fill(1, $doorsCount, 0); // [1 => false, 2 => false, 3 => false];
$random = unpack('C*', openssl_random_pseudo_bytes(2, $cstrong));
// door with a prize
$prizeDoor = 1 + array_pop($random) % $doorsCount;
// mark the prize door
$doors[$prizeDoor] = 1;
// player's choice
$firstChoice = 1 + array_pop($random) % $doorsCount;
// remaining doors
$remainingDoors = remove_door($doors, $firstChoice);
// host opens a door with a goat
$goatDoor = array_search(0, $remainingDoors);
assert($goatDoor != $firstChoice);
// remove a remaining door with a goat
$remainingDoors = remove_door($doors, $goatDoor);
// door with a prize behind must remain
assert(isset($remainingDoors[$prizeDoor]));
// alternative choice
$altChoice = key(remove_door($remainingDoors, $firstChoice));
assert($altChoice != $firstChoice);
assert($altChoice != $goatDoor);
// echo "First choice: $firstChoice\n";
// echo "Goat door: $goatDoor\n";
// echo "Second choice: $altChoice\n";
$primaryChoiceWon += $prizeDoor == $firstChoice;
$secondChoiceWon += $prizeDoor == $altChoice;
$chanceFirst = round($primaryChoiceWon/$tries, 2)*100;
$chanceSecond = round($secondChoiceWon/$tries, 2)*100;
echo "$tries: 1 => $chanceFirst%, 2 => $chanceSecond%\n";
//echo "$tries\t$chanceFirst\t$chanceSecond\n";
$tries += 1;
} while ($tries < 10000);
1: 1 => 0%, 2 => 100%
2: 1 => 0%, 2 => 100%
3: 1 => 0%, 2 => 100%
4: 1 => 25%, 2 => 75%
5: 1 => 40%, 2 => 60%
6: 1 => 33%, 2 => 67%
7: 1 => 43%, 2 => 57%
8: 1 => 38%, 2 => 63%
9: 1 => 44%, 2 => 56%
10: 1 => 50%, 2 => 50%
11: 1 => 45%, 2 => 55%
12: 1 => 42%, 2 => 58%
13: 1 => 38%, 2 => 62%
14: 1 => 43%, 2 => 57%
15: 1 => 40%, 2 => 60%
16: 1 => 44%, 2 => 56%
17: 1 => 41%, 2 => 59%
18: 1 => 39%, 2 => 61%
19: 1 => 37%, 2 => 63%
20: 1 => 40%, 2 => 60%
21: 1 => 38%, 2 => 62%
22: 1 => 36%, 2 => 64%
23: 1 => 35%, 2 => 65%
24: 1 => 38%, 2 => 63%
25: 1 => 40%, 2 => 60%
26: 1 => 42%, 2 => 58%
27: 1 => 41%, 2 => 59%
28: 1 => 39%, 2 => 61%
29: 1 => 41%, 2 => 59%
30: 1 => 40%, 2 => 60%
31: 1 => 39%, 2 => 61%
32: 1 => 41%, 2 => 59%
33: 1 => 42%, 2 => 58%
34: 1 => 44%, 2 => 56%
35: 1 => 43%, 2 => 57%
36: 1 => 44%, 2 => 56%
37: 1 => 43%, 2 => 57%
38: 1 => 42%, 2 => 58%
39: 1 => 44%, 2 => 56%
40: 1 => 43%, 2 => 58%
41: 1 => 41%, 2 => 59%
42: 1 => 40%, 2 => 60%
43: 1 => 42%, 2 => 58%
44: 1 => 41%, 2 => 59%
45: 1 => 40%, 2 => 60%
46: 1 => 39%, 2 => 61%
47: 1 => 38%, 2 => 62%
48: 1 => 38%, 2 => 63%
49: 1 => 37%, 2 => 63%
50: 1 => 36%, 2 => 64%
51: 1 => 35%, 2 => 65%
52: 1 => 35%, 2 => 65%
53: 1 => 34%, 2 => 66%
54: 1 => 35%, 2 => 65%
55: 1 => 36%, 2 => 64%
56: 1 => 38%, 2 => 63%
57: 1 => 37%, 2 => 63%
58: 1 => 38%, 2 => 62%
59: 1 => 37%, 2 => 63%
60: 1 => 37%, 2 => 63%
61: 1 => 36%, 2 => 64%
62: 1 => 37%, 2 => 63%
63: 1 => 37%, 2 => 63%
64: 1 => 36%, 2 => 64%
65: 1 => 35%, 2 => 65%
66: 1 => 35%, 2 => 65%
67: 1 => 34%, 2 => 66%
68: 1 => 34%, 2 => 66%
69: 1 => 33%, 2 => 67%
70: 1 => 33%, 2 => 67%
71: 1 => 32%, 2 => 68%
72: 1 => 32%, 2 => 68%
73: 1 => 32%, 2 => 68%
74: 1 => 31%, 2 => 69%
75: 1 => 31%, 2 => 69%
76: 1 => 32%, 2 => 68%
77: 1 => 32%, 2 => 68%
78: 1 => 32%, 2 => 68%
79: 1 => 32%, 2 => 68%
80: 1 => 31%, 2 => 69%
81: 1 => 31%, 2 => 69%
82: 1 => 30%, 2 => 70%
83: 1 => 30%, 2 => 70%
84: 1 => 31%, 2 => 69%
85: 1 => 31%, 2 => 69%
86: 1 => 30%, 2 => 70%
87: 1 => 30%, 2 => 70%
88: 1 => 31%, 2 => 69%
89: 1 => 31%, 2 => 69%
90: 1 => 31%, 2 => 69%
91: 1 => 32%, 2 => 68%
92: 1 => 32%, 2 => 68%
93: 1 => 32%, 2 => 68%
94: 1 => 32%, 2 => 68%
95: 1 => 32%, 2 => 68%
96: 1 => 31%, 2 => 69%
97: 1 => 31%, 2 => 69%
98: 1 => 31%, 2 => 69%
99: 1 => 30%, 2 => 70%
100: 1 => 30%, 2 => 70%
101: 1 => 30%, 2 => 70%
102: 1 => 29%, 2 => 71%
103: 1 => 30%, 2 => 70%
104: 1 => 31%, 2 => 69%
105: 1 => 30%, 2 => 70%
106: 1 => 30%, 2 => 70%
107: 1 => 30%, 2 => 70%
108: 1 => 30%, 2 => 70%
109: 1 => 30%, 2 => 70%
110: 1 => 31%, 2 => 69%
111: 1 => 31%, 2 => 69%
112: 1 => 30%, 2 => 70%
113: 1 => 30%, 2 => 70%
114: 1 => 31%, 2 => 69%
115: 1 => 30%, 2 => 70%
116: 1 => 30%, 2 => 70%
117: 1 => 30%, 2 => 70%
118: 1 => 30%, 2 => 70%
119: 1 => 29%, 2 => 71%
120: 1 => 29%, 2 => 71%
121: 1 => 29%, 2 => 71%
122: 1 => 30%, 2 => 70%
123: 1 => 29%, 2 => 71%
124: 1 => 29%, 2 => 71%
125: 1 => 29%, 2 => 71%
126: 1 => 29%, 2 => 71%
127: 1 => 28%, 2 => 72%
128: 1 => 29%, 2 => 71%
129: 1 => 29%, 2 => 71%
130: 1 => 29%, 2 => 71%
131: 1 => 29%, 2 => 71%
132: 1 => 29%, 2 => 71%
133: 1 => 29%, 2 => 71%
134: 1 => 29%, 2 => 71%
135: 1 => 29%, 2 => 71%
136: 1 => 29%, 2 => 71%
137: 1 => 29%, 2 => 71%
138: 1 => 29%, 2 => 71%
139: 1 => 29%, 2 => 71%
140: 1 => 29%, 2 => 71%
141: 1 => 30%, 2 => 70%
142: 1 => 30%, 2 => 70%
143: 1 => 31%, 2 => 69%
144: 1 => 31%, 2 => 69%
145: 1 => 30%, 2 => 70%
146: 1 => 30%, 2 => 70%
147: 1 => 30%, 2 => 70%
148: 1 => 30%, 2 => 70%
149: 1 => 30%, 2 => 70%
150: 1 => 30%, 2 => 70%
151: 1 => 30%, 2 => 70%
152: 1 => 30%, 2 => 70%
153: 1 => 30%, 2 => 70%
154: 1 => 30%, 2 => 70%
155: 1 => 30%, 2 => 70%
156: 1 => 30%, 2 => 70%
157: 1 => 30%, 2 => 70%
158: 1 => 30%, 2 => 70%
159: 1 => 30%, 2 => 70%
160: 1 => 30%, 2 => 70%
161: 1 => 30%, 2 => 70%
162: 1 => 30%, 2 => 70%
163: 1 => 30%, 2 => 70%
164: 1 => 30%, 2 => 70%
165: 1 => 30%, 2 => 70%
166: 1 => 31%, 2 => 69%
167: 1 => 31%, 2 => 69%
168: 1 => 32%, 2 => 68%
169: 1 => 31%, 2 => 69%
170: 1 => 32%, 2 => 68%
171: 1 => 32%, 2 => 68%
172: 1 => 31%, 2 => 69%
173: 1 => 32%, 2 => 68%
174: 1 => 32%, 2 => 68%
175: 1 => 31%, 2 => 69%
176: 1 => 32%, 2 => 68%
177: 1 => 32%, 2 => 68%
178: 1 => 31%, 2 => 69%
179: 1 => 32%, 2 => 68%
180: 1 => 32%, 2 => 68%
181: 1 => 32%, 2 => 68%
182: 1 => 32%, 2 => 68%
183: 1 => 32%, 2 => 68%
184: 1 => 32%, 2 => 68%
185: 1 => 31%, 2 => 69%
186: 1 => 31%, 2 => 69%
187: 1 => 31%, 2 => 69%
188: 1 => 31%, 2 => 69%
189: 1 => 31%, 2 => 69%
190: 1 => 32%, 2 => 68%
191: 1 => 32%, 2 => 68%
192: 1 => 32%, 2 => 68%
193: 1 => 33%, 2 => 67%
194: 1 => 32%, 2 => 68%
195: 1 => 33%, 2 => 67%
196: 1 => 33%, 2 => 67%
197: 1 => 33%, 2 => 67%
198: 1 => 33%, 2 => 67%
199: 1 => 33%, 2 => 67%
200: 1 => 33%, 2 => 67%
201: 1 => 33%, 2 => 67%
202: 1 => 33%, 2 => 67%
203: 1 => 33%, 2 => 67%
204: 1 => 33%, 2 => 67%
205: 1 => 33%, 2 => 67%
206: 1 => 33%, 2 => 67%
207: 1 => 33%, 2 => 67%
208: 1 => 33%, 2 => 67%
209: 1 => 33%, 2 => 67%
210: 1 => 33%, 2 => 67%
211: 1 => 33%, 2 => 67%
212: 1 => 33%, 2 => 67%
213: 1 => 33%, 2 => 67%
214: 1 => 33%, 2 => 67%
215: 1 => 33%, 2 => 67%
216: 1 => 33%, 2 => 67%
217: 1 => 33%, 2 => 67%
218: 1 => 33%, 2 => 67%
219: 1 => 33%, 2 => 67%
220: 1 => 34%, 2 => 66%
221: 1 => 34%, 2 => 66%
222: 1 => 34%, 2 => 66%
223: 1 => 34%, 2 => 66%
224: 1 => 34%, 2 => 66%
225: 1 => 34%, 2 => 66%
226: 1 => 34%, 2 => 66%
227: 1 => 33%, 2 => 67%
228: 1 => 34%, 2 => 66%
229: 1 => 34%, 2 => 66%
230: 1 => 34%, 2 => 66%
231: 1 => 34%, 2 => 66%
232: 1 => 34%, 2 => 66%
233: 1 => 33%, 2 => 67%
234: 1 => 33%, 2 => 67%
235: 1 => 33%, 2 => 67%
236: 1 => 33%, 2 => 67%
237: 1 => 33%, 2 => 67%
238: 1 => 33%, 2 => 67%
239: 1 => 33%, 2 => 67%
240: 1 => 33%, 2 => 67%
241: 1 => 33%, 2 => 67%
242: 1 => 33%, 2 => 67%
243: 1 => 33%, 2 => 67%
244: 1 => 33%, 2 => 67%
245: 1 => 33%, 2 => 67%
246: 1 => 33%, 2 => 67%
247: 1 => 33%, 2 => 67%
248: 1 => 33%, 2 => 67%
249: 1 => 33%, 2 => 67%
250: 1 => 34%, 2 => 66%
251: 1 => 33%, 2 => 67%
252: 1 => 33%, 2 => 67%
253: 1 => 33%, 2 => 67%
254: 1 => 33%, 2 => 67%
255: 1 => 33%, 2 => 67%
256: 1 => 33%, 2 => 67%
257: 1 => 33%, 2 => 67%
258: 1 => 33%, 2 => 67%
259: 1 => 33%, 2 => 67%
260: 1 => 33%, 2 => 67%
261: 1 => 33%, 2 => 67%
262: 1 => 33%, 2 => 67%
263: 1 => 33%, 2 => 67%
264: 1 => 33%, 2 => 67%
265: 1 => 34%, 2 => 66%
266: 1 => 34%, 2 => 66%
267: 1 => 34%, 2 => 66%
268: 1 => 34%, 2 => 66%
269: 1 => 34%, 2 => 66%
270: 1 => 34%, 2 => 66%
271: 1 => 34%, 2 => 66%
272: 1 => 34%, 2 => 66%
273: 1 => 34%, 2 => 66%
274: 1 => 34%, 2 => 66%
275: 1 => 34%, 2 => 66%
276: 1 => 34%, 2 => 66%
277: 1 => 34%, 2 => 66%
278: 1 => 33%, 2 => 67%
279: 1 => 34%, 2 => 66%
280: 1 => 34%, 2 => 66%
281: 1 => 33%, 2 => 67%
282: 1 => 34%, 2 => 66%
283: 1 => 34%, 2 => 66%
284: 1 => 34%, 2 => 66%
285: 1 => 34%, 2 => 66%
286: 1 => 34%, 2 => 66%
287: 1 => 33%, 2 => 67%
288: 1 => 34%, 2 => 66%
289: 1 => 34%, 2 => 66%
290: 1 => 33%, 2 => 67%
291: 1 => 33%, 2 => 67%
292: 1 => 33%, 2 => 67%
293: 1 => 33%, 2 => 67%
294: 1 => 33%, 2 => 67%
295: 1 => 33%, 2 => 67%
296: 1 => 33%, 2 => 67%
297: 1 => 33%, 2 => 67%
298: 1 => 33%, 2 => 67%
299: 1 => 32%, 2 => 68%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.