After seeing relevant lottery fraud reports, some people don’t understand what’s going on. . In order to prevent being defrauded by the lottery, we will use some simple examples to explain the deeper logic of the lottery, so that everyone can intuitively see what is hidden behind the lottery and prevent being deceived.
First, let’s implement a simple lottery logic. We need to determine the probability of each prize and draw it based on the probability.
There is a list of prizes. Each prize has a corresponding probability. Users draw prizes to obtain products with corresponding probabilities.
According to the above requirements, we create the corresponding prize table. We choose the probability from 1 to 100. Of course, you can also choose 0-1.
# 奖品列表和对应的概率,和为100 prizes = {"一等奖": 10,"二等奖": 20,"三等奖": 30,"参与奖": 40 }
This is a basic lottery logic. The difficulty in code implementation is mainly probability. How to implement probability and get the winning prize prize.
Regarding probability, we need to use the random module, that is, we need to use random numbers. Here I have two ways to choose from.
The lottery probability is realized by selecting intervals. The probability of the lottery is divided into several intervals, each interval corresponds to a prize, and then The winning interval is determined based on the range of random numbers. For example, we can divide the probability of the lottery into the following intervals:
- Interval 1: 0%~20%, corresponding to prize A
import random # 奖品列表和对应的概率 prizes = {"一等奖":10,"二等奖":20,"三等奖":30,"参与奖":40} #我们可以看做: # 一等奖:(0,10] # 二等奖:(10,30] # 三等奖:(30,60] # 参与奖:(60,100] # 抽奖函数 def lottery(): # 生成一个1~100之间的随机数 lucky_number = random.randint(0,100) # 初始化区间值和奖品名称 prob_sum_start = 0 prob_sum_end = 0 prize_name = None # 遍历奖品列表 for name, prob in prizes.items(): # 累加后一个区间值 prob_sum_end += prob # 如果随机数在计算区间内,则中奖 if prob_sum_start Copy after login
# 奖品列表和对应的概率,和为100 prizes = {"一等奖": 10,"二等奖": 20,"三等奖": 30,"参与奖": 40 }
import random # 奖品列表和对应的概率 prizes = {"一等奖": 10, "二等奖": 20, "三等奖": 30, "参与奖": 40} # 抽奖函数 def lottery(): # 生成一个1~100之间的随机数 lucky_number = random.randint(1, 100) print(lucky_number) # 初始化概率总和和奖品名称 prob_sum = 0 prize_name = None # 遍历奖品列表 for name, prob in prizes.items(): # 累加概率总和 prob_sum += prob # 如果随机数小于等于概率总和,则中奖 if lucky_number <= prob_sum: prize_name = name break return prize_name
import random # 奖品列表和对应的概率 prizes = {"一等奖": 10, "二等奖": 20, "三等奖": 30, "参与奖": 40} #白名单列表 whitelist = ["Tom", "Jerry", "Lucy"] # 抽奖函数 def lottery(white_user): # 生成一个1~100之间的随机数 lucky_number = random.randint(1, 100) print(lucky_number) # 初始化概率总和和奖品名称 prob_sum = 0 prize_name = None #判断在白名单内,中一等奖 if white_user in whitelist: return "恭喜您中一等奖" # 遍历奖品列表 else: for name, prob in prizes.items(): # 累加概率总和 prob_sum += prob # 如果随机数小于等于概率总和,则中奖 if lucky_number <= prob_sum: prize_name = name break return prize_name
import random # 奖品列表和对应的概率 prizes = {"一等奖": 10, "二等奖": 20, "三等奖": 30, "参与奖": 40} #已参加另外一个活动列表 active_user = [] #加法活动 def addition(name): active_user.append(name) return name+"参加了活动" # 抽奖函数 def lottery(name): # 生成一个1~100之间的随机数 lucky_number = random.randint(1, 100) # 初始化概率总和和奖品名称 prob_sum = 0 prize_name = None # 判断在白名单内,中一等奖 if name not in active_user: return "很抱歉,您没有资格参与活动" else: # 遍历奖品列表 for name, prob in prizes.items(): # 累加概率总和 prob_sum += prob # 如果随机数小于等于概率总和,则中奖 if lucky_number <= prob_sum: prize_name = name break return prize_name #测试一下 print(lottery("Tom"))#如果没有参与加法活动,来参与,无法抽奖 print(addition("Tom"))#Tom先来参与加法活动再去参与活动 print(lottery("Tom"))#参与活动抽奖
这种就需要和我们设置的随机范围撤上关系,我们的随机范围是1-100,比例为百分比1-100的整数,但是当我们加上小数之后,就不一样。比如:
1%和1.00%
那现在这两种概率你觉得是一样的吗?
答案肯定是不一样的,第一个是百分之一,第二个则是万分之一。因为第一个没有算小数,第二个还有两位小数,看似相等,其实已经变了。
如下图:这三个箭头指向,所占比例大概就是80%,20%,当我们数据越多,范围就会越广,如果随机,那数据会更容易落到80%。
抽奖概率正确性,我们其实可以从概率的统计定义中来看待。
在一定条件下,重复做n次试验,nA为n次试验中事件A发生的次数,如果随着n逐渐增大,频率nA/n逐渐稳定在某一数值p附近,则数值p称为事件A在该条件下发生的概率。
就是说数值越多,越是能证明概率的正确性。但是,我们抽奖只有一次机会。对于我来说80%肯定比20%更加容易随机。
代码控制:控制随机值,1-10000。
有机率估计是某王常用套路了,有机率基本等于没有。文字游戏算是被玩明白了。当然因为用户体量比较大,因此,也可能采用第4种方式,万分之一,或者是百万分之一。
这种机制就是比较明确的,整体意思就是,我已经做好坑你准备了,但是坑了我就收手,让你看到希望,给你大奖。常见的有抽了多少次,我就给你中什么奖品。
这里我们设置保底机制为10次必中一等奖。如果前9次都没有中奖,第10次一定中一等奖。
代码如下:
import random # 奖品列表和对应的概率 prizes = {"一等奖": 1, "二等奖": 2, "三等奖": 3, "参与奖": 94} lottery_num = 0 # 记录已经抽中的一等奖、二等奖、三等奖的次数 # 抽奖函数 def lottery(): # 生成一个1~100之间的随机数 global lottery_num lucky_number = random.randint(1, 100) print(lucky_number,lottery_num) # 初始化概率总和和奖品名称 prob_sum = 0 prize_name = None # 遍历奖品列表 for name, prob in prizes.items(): # 累加概率总和 prob_sum += prob # 如果随机数小于等于概率总和,则中奖 if lucky_number <= prob_sum: prize_name = name break if prize_name=="参与奖": lottery_num += 1 if lottery_num == 10: return "恭喜你,中一等奖" return prize_name else: lottery_num=0 return prize_name for i in range(10): print(lottery())
The above is the detailed content of How to implement lottery system in Python. For more information, please follow other related articles on the PHP Chinese website!