# 趣味编程：三门问题

 1 import random
2 import logging
3
4 class MontyHall(object):
5     def __init__(self, num=3):
6         '''
7         创建一个door列表
8         0表示门关闭的状态
9         1表示该门后有车
10         -1表示该门被主持人打开
11         '''
12         self.doors = [0] * num
13         self.doors[0] = 1
14         self.choice = -1
15         self.shuffle()
16
17     def shuffle(self):
18         '''
19         开始新的游戏
20         关闭所有打开的门（-1）
21         重新安排轿车的位置
22         '''
23         for i in range(len(self.doors)):
24             if self.doors[i] == -1:
25                 self.doors[i] = 0
26         random.shuffle(self.doors)
27
28     def makeChoice(self):
29         '''
30         player随机选择一扇门
31         '''
32         self.choice = random.randint(0, len(self.doors)-1)
33         logging.info('choice: %d' % self.choice)
34         logging.info('original: %s' % self.doors)
35
36     def excludeChoice(self):
37         '''
38         主持人排除选择
39         直到只剩两扇门
40         '''
41         toBeExcluded = []
42         for i in range(len(self.doors)):
43             if self.doors[i] == 0 and i != self.choice:
44                 toBeExcluded.append(i)
45
46         random.shuffle(toBeExcluded)
47         for i in range(len(self.doors)-2):
48             self.doors[toBeExcluded[i]] = -1
49         logging.info('final: %s' % self.doors)
50
51     def changeChoice(self):
52         '''
53         player改变选择
54         '''
55         toChange = []
56         for i in range(len(self.doors)):
57             if i != self.choice and self.doors[i] != -1:
58                 toChange.append(i)
59         self.choice = random.choice(toChange)
60         logging.info('choice changed: %d' % self.choice)
61
63         logging.info(self.doors)
64
65     def checkResult(self):
66         gotIt = False
67         if self.doors[self.choice] == 1:
68             gotIt = True
69         return gotIt

 1 def test(n):
2     result = {}
3     game = MontyHall()
4
5     for i in range(n):
6         game.shuffle()
7         game.makeChoice()
8         game.excludeChoice()
9
10         if game.checkResult():
11             result['yes'] = result.get('yes', 0) + 1
12         else:
13             result['no'] = result.get('no', 0) + 1
14
15     for key in result:
16         print('%s: %d' % (key, result[key]))
17
18
19
20 if __name__ == '__main__':
21     logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
22     test(10000)
View Code
yes: 3304
no: 6696

 1 def test(n):
2     result = {}
3     game = MontyHall(3)
4
5     for i in range(n):
6         game.shuffle()
7         game.makeChoice()
8         game.excludeChoice()
9         # 改变选择
10         game.changeChoice()
11
12         if game.checkResult():
13             result['yes'] = result.get('yes', 0) + 1
14         else:
15             result['no'] = result.get('no', 0) + 1
16
17     for key in result:
18         print('%s: %d' % (key, result[key]))
19
20
21
22 if __name__ == '__main__':
23     logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
24     test(10000)
View Code
yes: 6691
no: 3309

 1 def test(n):
2     result = {}
3     game = MontyHall(50)
4
5     for i in range(n):
6         game.shuffle()
7         game.makeChoice()
8         game.excludeChoice()
9         game.changeChoice()
10
11         if game.checkResult():
12             result['yes'] = result.get('yes', 0) + 1
13         else:
14             result['no'] = result.get('no', 0) + 1
15
16     for key in result:
17         print('%s: %d' % (key, result[key]))
18
19
20
21 if __name__ == '__main__':
22     logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
23     test(10000)
View Code
yes: 9794
no: 206

posted @ 2013-11-24 20:28 CaliforniaDream 阅读(...) 评论(...) 编辑 收藏