From 110d346d390c6dba07ac6c169a6c150cd78734d4 Mon Sep 17 00:00:00 2001 From: zhang Date: Sun, 5 Apr 2020 23:08:07 +0800 Subject: =?UTF-8?q?=E4=B8=89=E9=97=A8=E9=97=AE=E9=A2=98=E7=9A=84python?= =?UTF-8?q?=E4=BB=BF=E7=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stats/three_gates.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 stats/three_gates.py 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() -- cgit v1.2.3