05.编程理解小数和大数定律
1 import random 2 import matplotlib.pyplot as plt 3 4 5 # 掷硬币,头0,花1 6 def toss(): 7 return random.randint(0, 1) 8 9 10 if __name__ == "__main__": 11 12 indices = [] 13 freq = [] 14 for toss_num in range(10, 10001, 10): 15 16 heads = 0 17 for _ in range(toss_num): 18 if toss() == 0: 19 heads += 1 20 freq.append(heads / toss_num) 21 indices.append(toss_num) 22 23 plt.plot(indices, freq) 24 plt.show()