diff options
| author | zhang <zch921005@126.com> | 2020-04-05 23:08:07 +0800 |
|---|---|---|
| committer | zhang <zch921005@126.com> | 2020-04-05 23:08:07 +0800 |
| commit | 110d346d390c6dba07ac6c169a6c150cd78734d4 (patch) | |
| tree | e8dfc1a9fa90b70fa7d47eb096b4df95f4ed2486 | |
| parent | 71e4aa85367ef9af880204f13029211f16ff4f80 (diff) | |
三门问题的python仿真
| -rw-r--r-- | stats/three_gates.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/stats/three_gates.py b/stats/three_gates.py new file mode 100644 index 0000000..4d6433a --- /dev/null +++ b/stats/three_gates.py @@ -0,0 +1,59 @@ +import random + +import matplotlib.pyplot as plt + +gates = ['coat', 'coat', 'car'] + + +def not_change(N=1000): + win = 0 + for i in range(N): + random.shuffle(gates) + # print(gates) + chosed = random.randint(0, 2) + print('chosed {}({})'.format(chosed, gates)) + unchosed_coat = random.choice([i for i in range(3) if i != chosed and gates[i] != 'car']) + print('{} is a coat, change or not'.format(unchosed_coat)) + final_chose = chosed + if gates[final_chose] == 'car': + win += 1 + print('not change, win') + else: + print('not change, lose') + return win + + +def change(N=1000): + win = 0 + for i in range(N): + random.shuffle(gates) + # print(gates) + chosed = random.randint(0, 2) + print('chosed {}({})'.format(chosed, gates)) + unchosed_coat = random.choice([i for i in range(3) if i != chosed and gates[i] != 'car']) + print('{} is a coat, change or not'.format(unchosed_coat)) + for i in range(3): + if i != chosed and i != unchosed_coat: + final_chose = i + if gates[final_chose] == 'car': + print('change, win') + win += 1 + else: + print('change, lose') + return win + + +if __name__ == '__main__': + # N = 100000 + # # print('not change, win {}/{}'.format(not_change(N), N)) + # print('change, win {}/{}'.format(change(N), N)) + + not_change_rates = [] + change_rates = [] + + for n in [10, 100, 1000, 10000, 100000, 100000]: + not_change_rates.append(not_change(n) / n) + change_rates.append(change(n) / n) + + plt.plot(range(6), not_change_rates, range(6), change_rates) + plt.show() |
