我正在学习 python,但从我的角度来看,我发现了一些不直观的东西。我试图根据彩票的输出打印高斯曲线。在该程序中,我可以设置开奖范围、一场比赛的开奖次数和比赛次数。我总结了每场比赛的平局结果。我记录结果出现了多少次,并根据这些数据绘制图表。
当我在游戏中设置one draw,那么每个值的概率都是相同的。它在附图中以红色可见。我预料到了。
当我设置三次或更多抽奖时,中间值概率很高。例如,如果我在 0 到 100 的范围内绘制了 3 个图形,那么我可以预期值的总和在 0 到 300 的范围内,最可能的值是 150。当我在图形上绘制时,我得到高斯曲线.它在图中是蓝色的。
非直观的情况是当我设置两次抽奖。我预计该曲线将与之前的情况相同,但我看到输出类似于三角形。它是绿色曲线。
问题是:
两次和多次绘制之间的根本区别是什么?为什么输出曲线不同?
为什么我设置了两次绘制然后我不会得到高斯曲线?
Python代码:
import random
import matplotlib.pyplot as plt
import collections
class GaussGame():
def __init__(self, draw_range = {min: 0, max: 100}, number_of_draws = 5, number_of_games = 100000) -> None:
self.draw_range = draw_range
self.number_of_draws = number_of_draws
self.number_of_games = number_of_games
def start(self):
#Create win dictionary which contains amounts of possible wins as a key and, number of wins for each possible amounts as a value.
win_dict = collections.OrderedDict()
for x in range(self.draw_range[min]*self.number_of_draws, self.draw_range[max]*self.number_of_draws+1):
win_dict[x]=0
#Loop for all games
for x in range(self.number_of_games):
#Loop for one game
d_sum = 0 #Sum of the drawn values
d_sum
for x in range(self.number_of_draws):
d_sum += random.randrange(self.draw_range[min], self.draw_range[max]+1)
win_dict[d_sum] += 1
return win_dict
def main():
#When I run game several times, with different number_of_draws parameter and draw it on one graph, then I can get interesting picture :-D
g1 = GaussGame({min: 0, max: 100},1,10000000)
g2 = GaussGame({min: 0, max: 100},2,10000000)
g3 = GaussGame({min: 0, max: 100},3,10000000)
g4 = GaussGame({min: 0, max: 100},4,10000000)
g5 = GaussGame({min: 0, max: 100},5,10000000)
d1 = g1.start()
d2 = g2.start()
d3 = g3.start()
d4 = g4.start()
d5 = g5.start()
plt.plot(d1.keys(), d1.values(), 'r.')
plt.plot(d2.keys(), d2.values(), 'g.')
plt.plot(d3.keys(), d3.values(), 'b.')
plt.plot(d4.keys(), d4.values(), 'b.')
plt.plot(d5.keys(), d5.values(), 'b.')
plt.show()
if __name__ == "__main__":
main()