Python - 彩票模拟器,模拟彩票的摇奖,并计算中奖概率,保本概率
程序员文章站
2022-07-02 23:46:19
文章目录Python - 彩票模拟器,模拟彩票的摇奖,并计算中奖概率,保本概率1、代码2、运行结果3、玩法Python - 彩票模拟器,模拟彩票的摇奖,并计算中奖概率,保本概率1、代码# 概率发生器import random# 配置class Config: # 是否打印选配结果 print_achievement = False # 测试轮次 round_number = 100 # 用户投注 sale = 12 # 玩法...
Python - 彩票模拟器,模拟彩票的摇奖,并计算中奖概率,保本概率
1、代码
# 概率发生器
import random
# 配置
class Config:
# 是否打印选配结果
print_achievement = False
# 测试轮次
round_number = 100
# 用户投注
sale = 12
# 玩法
class PlayWay:
# 开始数字
start = 1
# 结束数字
end = 12
# 玩法开出几个数字
machine_choose = 5
# 用户选择几个数字
user_choose = 8
# 最大赔率
max_odds = 12.5
# 平均赔率
avg_odds = 12.5
# 生成随机票
def rg(q):
u_list = []
for i in range(q):
ur = random.randint(Config.PlayWay.start, Config.PlayWay.end)
if ur not in u_list:
u_list.append(ur)
else:
while ur in u_list:
ur = random.randint(Config.PlayWay.start, Config.PlayWay.end)
if ur not in u_list:
u_list.append(ur)
break
return u_list
if __name__ == '__main__':
# 投赔率
sale_odds_rate = Config.sale / Config.PlayWay.max_odds
# 总注数
count_sale = Config.round_number * Config.sale
# 总成功次数
count_success = 0
for x in range(Config.round_number):
if Config.print_achievement:
print('-' * 15, 'round', x + 1, '-' * 15)
m = rg(Config.PlayWay.machine_choose)
if Config.print_achievement:
print('machine', m)
# 成功计次
count = 0
for j in range(Config.sale):
u = rg(Config.PlayWay.user_choose)
flag = False
# 当玩法变化从少选到到复式时,包含方式相反
if Config.PlayWay.machine_choose >= Config.PlayWay.user_choose:
flag = set(u) <= set(m)
else:
flag = set(u) > set(m)
if flag:
if Config.print_achievement:
print('success', u)
count += 1
else:
if Config.print_achievement:
print('fail', u)
count_success += count
if Config.print_achievement:
print('success count', count)
# 胜率
print('success rate', format(count_success / count_sale * 100, '0.4f') + '%')
# 保本率
print('break even rate', format(count_success / count_sale * 100 * Config.PlayWay.max_odds, '0.4f') + '%')
2、运行结果
运行结果基本稳定在 4.5%-4.7% 左右
下面是运行12万注的结果
success rate 4.5083%
break even rate 56.3542%
3、玩法
可以根据不同的玩法设置不同的类型
如下是快乐彩12选5 任3的玩法配置
# 玩法
class PlayWay:
# 开始数字
start = 1
# 结束数字
end = 12
# 玩法开出几个数字
machine_choose = 5
# 用户选择几个数字
user_choose = 3
# 最大赔率
max_odds = 12.5
# 平均赔率
avg_odds = 12.5
本文地址:https://blog.csdn.net/qq_15071263/article/details/107323948
上一篇: 使用tkinter实现三子棋游戏
下一篇: 荐 Python菜鸟入门:day06