How to implement lottery system in Python

王林
Release: 2023-05-11 09:16:05
forward
5275 people have browsed it

    1. Article Topic

    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.

    2. Lottery design ideas

    First, let’s implement a simple lottery logic. We need to determine the probability of each prize and draw it based on the probability.

    Step One: Requirements

    There is a list of prizes. Each prize has a corresponding probability. Users draw prizes to obtain products with corresponding probabilities.

    Step 2: Create a prize list

    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 }
    Copy after login
    Copy after login

    The third step: Probability implementation

    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 first method: Realize through intervals

    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

    • ## - Interval 2: 20%~50%, corresponding to prize B

    • - Interval 3: 50%~80%, corresponding to prize C

    • - Interval 4: 80%~100%, corresponding to prize D

    When the user draws a lottery, we can generate a random number between 0 and 100, and then determine the winning number based on the interval of the random number. of prizes. For example, if the generated random number is 35, then the winning interval will be 2, corresponding to prize B.

    Advantages: The advantage of realizing the lottery probability in this way is that the winning probability of each prize can be flexibly controlled, and it can also be adjusted according to the actual situation.

    Disadvantages: The sum of the winning probabilities of each prize must be equal to 100%, otherwise the lottery results will be inaccurate.

    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

    Second type: Implementation through accumulation of prize probabilities
    We can implement the lottery through probability accumulation, first generate a random number between 1 and 100 , indicating the result of this lottery.

    Then the results are accumulated sequentially according to the probability of the prize until the accumulated result is greater than or equal to the random number.

    The probability of the prizes that stops accumulating at the end is the prize won in this draw.

    For example, if the random number is 50, according to the probability accumulation method, the following result can be obtained:

    # 奖品列表和对应的概率,和为100 prizes = {"一等奖": 10,"二等奖": 20,"三等奖": 30,"参与奖": 40 }
    Copy after login
    Copy after login

    10< 50, not the first prize;

    10 20 = 30 < 50, not the second prize;

    10 20 30 = 60 >= 50, is the third prize.

    Therefore, the prize won in this draw is the third prize.

    Advantages: The probability of prizes can be flexibly adjusted to achieve different lottery effects. This method can also be applied to multiple prizes. It only needs to be accumulated according to the corresponding probability. It is simple to understand and easy to implement.

    Disadvantage: The disadvantage is that the cumulative probability of each prize needs to be calculated. If the number of prizes is large, the calculation amount may be large.

    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
    Copy after login

    Step 4: Add special mechanism

    After we participate in certain activities or certain draws, we will always be tempted and be stunned by the big prizes Mind, we don’t know what the lottery mechanism is, and how to solve it. Previously, we could let each prize be drawn according to the set prize probability. At this time, we need to add some special mechanisms. These mechanisms are mechanisms for being "scammed", or in other words, mechanisms for being "arranged".

    The first one: Let whoever you want to win the prize win the prize
    This method is mainly used in party prank occasions. Everyone should pay attention to this. Don't do it online, because you don't know who wrote this lottery program.

    For this method, we need to add a whitelist. If it is judged to be a person on the whitelist, then we will let him plant the designated prize.

    The code is as follows:

    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
    Copy after login

    The second type: you can participate in another activity before you can come to the lottery
    This method increases the verification of qualifications , or it can be that you can only participate in the lottery after you have user information, which can be regarded as adding some security restrictions.

    If the user participates in another activity or logs in, he can participate in the activity and enter the activity process.

    The code is as follows:

    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"))#参与活动抽奖
    Copy after login

    The third type: I won’t let you win even if I beat you to death
    This kind of activity is purely done by disgusting people , but all participating users have no idea at all, and will all think that it is their bad luck. The grand prize is there, but you can’t get it.

    This does not use probabilities and gives you the results directly. This is completely his marketing method.

    The code is not as good as..., there is no need, just return it.

    第四种:概率都是百分比,但是效果不一样

    这种就需要和我们设置的随机范围撤上关系,我们的随机范围是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())
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!