为了模拟这个行为,我们首先要有一个购奖号码,就是你要买的那串数字,还有一个开奖号码。
那就让它随机生成两组数进行匹配。按照规则,红球的范围是1-33,蓝球的范围是1-16。
红球6个数,蓝球1个数,红球加蓝球一共7个数。
附上百度百科的各个奖项的中奖率:
一等奖(6+1)中奖概率为:红球33选6乘以蓝球16选1=1/17721088=0.0000056%;
二等奖(6+0)中奖概率为:红球33选6乘以蓝球16选0=15/17721088=0.0000846%;
三等奖(5+1)中奖概率为:红球33选5乘以蓝球16选1=162/17721088=0.000914%;
四等奖(5+0、4+1)中奖概率为:红球33选5乘以蓝球16选0=7695/17721088=0.0434%;
五等奖(4+0、3+1)中奖概率为:红球33选4乘以蓝球16选0=137475/17721088=0.7758%;
六等奖(2+1、1+1、0+1)中奖概率为:红球33选2乘以蓝球16选1=1043640/17721088=5.889%;
共计中奖率:6.71%。
话不多说,先上代码:
import random
#定义了一个字典`countnum`,用来统计每个奖项和未中奖的次数,初始值都为0
countnum = {
'一等奖': 0,
'二等奖': 0,
'三等奖': 0,
'四等奖': 0,
'五等奖': 0,
'六等奖': 0,
'未中奖': 0
}
#定义了一个字典`countsave`,用来存储每个奖项的中奖号码和购奖号码,初始值都为空列表。
countsave = {
'一等奖': {
'中奖号码':[],
'购奖号码':[]
},
'二等奖': {
'中奖号码':[],
'购奖号码':[]
},
'三等奖': {
'中奖号码':[],
'购奖号码':[]
},
'四等奖':{
'中奖号码':[],
'购奖号码':[]
},
'五等奖':{
'中奖号码':[],
'购奖号码':[]
},
'六等奖':{
'中奖号码':[],
'购奖号码':[]
}
}
for i in range(10):
#定义一个函数`generate_lottery_numbers`,用于生成一组随机的彩票号码。红球从1到33中随机抽取6个,不重复,并排序;蓝球从1到16中随机抽取1个
def generate_lottery_numbers():
red_balls = random.sample(range(1, 34), 6)
red_balls.sort()
blue_ball = random.randint(1, 16)
return red_balls, blue_ball
#定义一个函数`match_numbers`,用于比较两组彩票号码,计算匹配结果,并更新相应的奖项号码列表。根据匹配的数量和蓝球是否相同,返回一个计数值count
def match_numbers(red_balls1, blue_ball1, red_balls2, blue_ball2):
count = 0
if (blue_ball1 == blue_ball2 and len(set(red_balls1).intersection(red_balls2)) == 2) or (blue_ball1 == blue_ball2 and red_balls1 == red_balls2) or blue_ball1 == blue_ball2:
count = 1
countsave['六等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['六等奖']['购奖号码'].append([red_balls1,blue_ball1])
elif len(set(red_balls1).intersection(red_balls2)) == 4 or (len(set(red_balls1).intersection(red_balls2)) == 3 and blue_ball1 == blue_ball2):
count = 2
countsave['五等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['五等奖']['购奖号码'].append([red_balls1,blue_ball1])
elif len(set(red_balls1).intersection(red_balls2)) == 5 or (len(set(red_balls1).intersection(red_balls2)) == 4 and blue_ball1 == blue_ball2):
count = 3
countsave['四等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['四等奖']['购奖号码'].append([red_balls1,blue_ball1])
elif len(set(red_balls1).intersection(red_balls2)) == 5 and blue_ball1 == blue_ball2:
count = 4
countsave['三等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['三等奖']['购奖号码'].append([red_balls1,blue_ball1])
elif len(set(red_balls1).intersection(red_balls2)) == 6:
count = 5
countsave['二等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['二等奖']['购奖号码'].append([red_balls1,blue_ball1])
elif len(set(red_balls1).intersection(red_balls2)) == 6 and blue_ball1 == blue_ball2:
count = 6
countsave['一等奖']['中奖号码'].append([red_balls2,blue_ball2])
countsave['一等奖']['购奖号码'].append([red_balls1,blue_ball1])
return count
# 调用`generate_lottery_numbers`函数来随机生成购奖号码
red_balls1, blue_ball1 = generate_lottery_numbers()
print("您的号码","红球:", red_balls1, "蓝球:", blue_ball1)
# 调用`generate_lottery_numbers`函数来随机生成开奖号码
red_balls2, blue_ball2 = generate_lottery_numbers()
print("开奖号码","红球:", red_balls2, "蓝球:", blue_ball2)
# 调用`match_numbers`函数将开奖号码与购奖号码进行匹配,并打印匹配结果。
count = match_numbers(red_balls1, blue_ball1, red_balls2, blue_ball2)
print("匹配结果:", count)
# 定义了一个字典`winning_conditions`,存储每个对应的中奖信息
winning_conditions = {
0: "很遗憾,您没有中奖。",
1: "恭喜您中了六等奖!",
2: "恭喜您中了五等奖!",
3: "恭喜您中了四等奖!",
4: "恭喜您中了三等奖!",
5: "恭喜您中了二等奖!",
6: "恭喜您中了一等奖!",
}
# 根据匹配结果的计数值返回相应的中奖信息,并打印中奖信息揭示模拟结果
winning_message = winning_conditions.get(count, "很遗憾,您没有中奖。")
print(winning_message)
print("\n")
# 定义了一个函数`count_wins`,用于根据匹配结果的计数值更新每个奖项的中奖次数,然后调用该函数更新`countnum`字典。
def count_wins(count):
if count == 0:
countnum['未中奖'] += 1
elif count == 1:
countnum['六等奖'] += 1
elif count == 2:
countnum['五等奖'] += 1
elif count == 3:
countnum['四等奖'] += 1
elif count == 4:
countnum['三等奖'] += 1
elif count == 5:
countnum['二等奖'] += 1
elif count == 6:
countnum['一等奖'] += 1
return countnum
countnum = count_wins(count)
print("获奖情况:",countnum)
# 打印存储的奖项信息
for key, value in countsave.items():
print(f'{key}:') # 默认情况下,print函数会自动添加换行符
for sub_key, sub_value in value.items():
print(f'\t{sub_key}: {sub_value}')
我自己循环了一千万次,发现一个一等奖都没有。感兴趣的朋友可以运行一下代码体验一下。