
Monte Carlo simulation of the Monty Hall Problem in Ruby and Python - acangiano
http://antoniocangiano.com/2009/01/01/monte-carlo-simulation-of-the-monty-hall-problem-in-ruby-and-python/
======
axiom
Wow, that's a lot of code.

    
    
      def main(iter=100000):
    	stay_win = 0.0
    	switch_win = 0.0
    	for i in xrange(iter):
    		win_door = int(numpy.random.uniform()*3)
    		pick_door = int(numpy.random.uniform()*3)
    		if pick_door == win_door: 
    			stay_win += 1.0
    		else: 
    			switch_win += 1.0
    	print 'stay: ', stay_win/float(iter)
    	print 'switch: ', switch_win/float(iter)
    	return

~~~
aston
Your code doesn't really provide "proof." You've completely omitted the host's
door elimination. I know in the end it doesn't matter, but if you already knew
the answer you would code the simulation in the first place, right?

~~~
tc
Not that this really needs another implementation, but since I already wrote
it before seeing this thread...

    
    
      (defun monty-hall (strategy &aux (winner (random 3)) (choice (random 3)))
        (let ((monty (if (= winner choice)
                         (do ((x (random 3) (random 3)))
                             ((/= x winner) x))
                         (- 3 (+ winner choice)))))
          (case strategy
            (:stay (= winner choice))
            (:change (= winner (- 3 (+ choice monty)))))))
    

_(looping and reporting omitted)_

Incidentally, I think the code in the original article provides a good example
of how code can be so verbose as to detract from understanding. Good code
clarifies a problem rather than obfuscating it under the weight of useless
detail.

