前面写了一篇文章用来计算快乐8中一等奖的概率,后来有人发私信问我,怎么计算其他情况的中奖概率,于是我把其他情况也写了一下。
# python3.8
# author: 天天卡丁
from scipy.special import comb
# 因时间有限,只记录了部分情况的中奖奖金
lottery_bonus = {(10,10):5000000,(10,9):8000 , (10,8):800,(10,7):80,(10,6):5, (10,5):3, (10,0):2
,(9,9):300000 ,(9,8):2000, (9,7):200, (9,6):20, (9,5):5, (9,4):3, (9,0):2
,(8,8):50000 ,(8,7):800, (8,6):88, (8,5):10 ,(8,4):3, (8,0):2
,(7,7):10000 ,(7,6):288, (7,5):28, (7,4):4 , (7,0):2}
def prob(n,m):
# 计算选n个中m个的概率,比如选9中9,或选9中8
all_reslut = comb(80,n)
if m == 0:
frst_prize = comb(60,n)
elif m<n:
frst_prize = comb(20,m)*comb(60,n-m)
elif m==n:
frst_prize = comb(20,m)
elif n>m:
print("请输入合理的n和m")
else:
pass
f = int(all_reslut/frst_prize)
print(f"选{n}中{m}的概率为:{f}分之一")
if lottery_bonus.get((n,m)):
expect = round(lottery_bonus[(n,m)]/f,2)
print(f"选{n}中{m}的中奖期望为{expect}元")
prob(9,7)
这样,通过不同的参数n和m可以计算出所有情况的中奖概率以及部分奖项的中奖期望。