4/29/13

The Monty Hall problem, and why I learned to love programming.

here's a script for a youtube video I want to create:

web browser opens to the Monty Hall problem on wikipedia

♖ : Hm.. the Monty Hall problem.. so there's a prize behind one of three doors, and I get to pick one, but before opening it, one of the other doors is opened to reveal a goat, and I get to switch my guess if I want. Seems like it wouldn't matter, but they say I should. I'm skeptical.

scrolls down to solutions

♖ : Ok, here we have lots of crazy diagrams and heartfelt arguments about why I should believe what they're saying. But I've seen smart people be wrong before. This sounds like a job for programming.

web browser opens to a javascript read-eval-print-loop program, like jsFiddle

♖ : Ok, we have a prize (types: var prize) behind one of three doors (types: = Math.ceil(Math.random() * 3)). Next, we choose a door (types: var guess = Math.ceil(Math.random() * 3)). But before it opens, Monty Hall opens one of the other doors to reveal a goat (types: var open = Math.ceil(Math.random() * 3)), of course, Monty Hall wouldn't open the prize door, so if we pick that one, we need to pick again (types: if (open == prize) continue), where that continue will repeat a loop (puts a while-true loop around the previous two lines). Monty Hall also wouldn't open the door we guessed (types: if (open == guess) continue), and if neither of those bad things happen, we can break from the loop (types: break). Great, now let's print out where the prize was (types: if (guess == prize) print('prize behind guessed door') else print('prize behind remaining door')). Now let's run it a few times.

runs program a few times, and it outputs a sequence of 'prize behind guessed door' and 'prize behind remaining door'

♖ : Hm.. if I squint, I might be able to convince myself that one thing is happening more often than the other.. let's run this a million times (adds for (var i = 0; i < 1000000; i++) around code), and it's probably best to count rather than print out what happens each time (adds var prizeBehindGuessedDoor = 0 and var prizeBehindRemainingDoor = 0 to top, and changes print statements to prizeBehindGuessedDoor++ and prizeBehindRemainingDoor++), and we'll want to know what those numbers are at the end (adds print('prize behind guessed door count: ' + prizeBehindGuessedDoor) and print('prize behind remaining door count: ' + prizeBehindRemainingDoor))

runs program, and it prints out something like: prize behind guessed door count: 333, prize behind remaining door count: 667

♖ : Hm.. kinda seems like it's better to pick the remaining door. I guess they were right. This time. Thankyou programming.