概率论与数理统计--抛硬币试验

方法一

from collections import Counter
import matplotlib.pyplot as plt
import random
times=10000
#模拟随机抛10000次硬币
count=[]
for i in range(1,times):
    y=random.randint(0,1)
    count.append(y)

#统计正面出现的次数
a=Counter(count)
f1=a[1]
print(f1)
#10000次中正面出现的概率
print(f1/times)
#画图展示1-10000次试验中,每次正面向上出现的频率
p=[]
indices=[]
for i in range(1,times):
    heads=0
    for j in range(i):
        if count[j]==1:
            heads+=1
    p.append(heads/i)
    indices.append(i)
plt.plot(indices,p)
plt.show()

方法二

import matplotlib.pyplot as plt
import random
def r():
    s=random.randint(0,1)
    return s
times=5001
indices=[]
f=[]
for i in range(1,times):
    heads=0
    for j in range(i):
        if r()==1:
            heads+=1
    f.append(heads/i)
    indices.append(i)
print(heads/times)
plt.plot(indices,f)
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wp猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值