Skip navigation.
Home
Freedom is contagious.

Eric Levigne dubbed "Fastest Codeslinger 2006"

Taking only 9 minutes to solve the classic Gameshow Problem Eric Levigne showed us how it's done with Common Lisp.



Here is restatement of the problem. You are on a game show. In front of you are three doors. Behind one of those doors is an all expense paid college education! Behind the other two doors are goats. The host asks you to select one of the doors. You point to a door. After you make your selection, the host opens one of the other two doors to reveal a goat. He then asks you if you'd like to stick to your original decision or switch to the other unopened door. What should you do? 1. Switch 2. Stay with original choice. 3. It does not matter. Your program will show the one correct answer via simulation or algorithm. You must post both your code and your output showing the correct answer to the website.

The correct answer to the problem is that you should always switch. Don't believe it? Well, run the code for yourself.

The Winning Code:

; you should switch

(defvar *switch* 0)
(defvar *total* 0)

; doors are 0, 1, 2
; always choose 0
; if 0 was right, then switching loses


(dotimes (i 500)
   (let ((num (random 3)))
      (incf *total*)
      (unless (eql num 0) (incf *switch*))))

(print "switching")
(print *switch*)
(print "stay")
(print (- *total* *switch*))
(if (> *switch* (/ *total* 2))
    (print "you should switch")
    (print "you should stick with original"))


JC Jones, our judge for the contest, has an interesting discussion about the problem on his website.

Finally, our Information Minister, Jim Wilson, weighs in with this:

Date: Wed, 15 Feb 2006 21:45:43 -0500
Reply-To: Platform Independent Linux List! <[log in to unmask]>
Sender: Platform Independent Linux List! <[log in to unmask]>
From: Jim Wilson <[log in to unmask]>
Subject: Eric is the MAN!

Congratulations to Eric for his CLISP solution to Clint's simulation problem. I spent all my time looking at Python's documentation of its "random" module for "shuffle()" and "randint()". Meanwhile Eric cleaned our clocks slinging those parentheses!

When I got home, I finished my simulation. Eric's was **way** more clever. Here's my pitiful attempt; it does confirm the counterintuitive result.

#  Warning -- Python code follows 

from random import shuffle, randint

doors = [ "goat", "goat", "full scholarship" ]

swap = 0   # Wins when swapping
stay = 0   # Wins when sticking with original pick.

for i in xrange(1000):  # Run 1000 trials
  shuffle(doors)        #   Load the doors with prizes
  p = randint(0, 2)     #   Pick a random door
  h = randint(0, 2)     #   Let host pick a random door
  while (h != p and     #   Until he finds one that
  doors[h] != "goat"):  #   has a goat behind it
    h = randint(0, 2)   #     (the one he will expose)
  for s in xrange(3):   #   S is door we would swap to
    if (s != h and
    s != p): break
  if doors[p] == "full scholarship": stay += 1  # If sticking won, ...
  if doors[s] == "full scholarship": swap += 1  # If swapping won, ...

print "swap wins", swap, "times"
print "stay wins", stay, "times"