The Monty Hall Problem

I love when smart people get things wrong. And one of my favorite examples of this is the Monty Hall problem. I first learned about it in the book The Curious Incident of the Dog in the Nighttime. And I remember finding this post online explaining what happened to Marilyn vos Savant when she ‘solved’ the problem. I love reading the letters she got!

One of the best explanations of the problem was given in a Horizon episode, one of those episodes with Alan Davis. You can watch it below:

So I wrote a simple test, similar to the one BBC did. Only this time through code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function runSimulation (numGames)
	--create games
	local games = {}
 
	local i = 1
	while ( i <= numGames )
	do
		local g = {0,0,0}
 
		g [  math.random(3) ] = 1
 
		table.insert(games, g)
 
		i = i + 1
	end
 
 
	--play game
	local winsWithSwitch = 0
	local wins = 0
	local i = 1
 
	while( i <= numGames )
	do
		local game = { unpack(games[i]) } 
		--print (games[i][0])
 
		local pick =  math.random(3)
		local result = game[pick]
		table.remove  (game, pick)
 
		local loss = findIndexFor ( game, 0) 
		if loss ~= 0 then
			table.remove  (game, loss)
 
 
			if game[1] == 1 then
				winsWithSwitch = winsWithSwitch + 1
			else
				wins = wins + 1
			end
		end
		i = i + 1
	end
 
	print("WINS: "..wins)
	print("WINS WITH SWITCH: "..winsWithSwitch)
end 
 
function findIndexFor (table, value)
	for i = 1, #table do
		if table[i] == 0 then
			return i
		end
    end
    return 0
end
 
runSimulation(1000)

And I asked a friend of mine to write the same test in a functional language. He used F#. You can see his post about it here.