Slashdot Mirror


User: davejohncole

davejohncole's activity in the archive.

Stories
0
Comments
1
First seen
Last seen
Profile
(view on slashdot.org)

Comments · 1

  1. Re:So I didn't randomly simulate it on Psychologists Don't Know Math · · Score: 1

    Here is my very dodgey Python implementation to simulate the problem.  It it clearly in your favour to switch doors.

    % cat monty.py
    import random

    class Game:
        def __init__(self):
            self.doors = ['car', 'goat', 'goat']
            random.shuffle(self.doors)

        def first_choice(self):
            self.choice = random.randint(0, 2)

        def monty_choice(self):
            door_nums = [0, 1, 2]
            door_nums.remove(self.choice)
            choice = random.choice(door_nums)
            if self.doors[choice] == 'goat':
                self.monty_choice = choice
            else:
                door_nums.remove(choice)
                self.monty_choice = door_nums[0]

        def switch(self):
            door_nums = [0, 1, 2]
            door_nums.remove(self.choice)
            door_nums.remove(self.monty_choice)
            self.choice = door_nums[0]

        def is_winner(self):
            return self.doors[self.choice] == 'car'

    wins = 0
    for i in range(1000):
        g = Game()
        g.first_choice()
        g.monty_choice()
        if g.is_winner():
            wins += 1

    print 'Do not switch won %d times = %.0f%%' % (wins, wins * 100.0 / 1000)

    wins = 0
    for i in range(1000):
        g = Game()
        g.first_choice()
        g.monty_choice()
        g.switch()
        if g.is_winner():
            wins += 1

    print 'Switch won %d times = %.0f%%' % (wins, wins * 100.0 / 1000)