SoFunction
Updated on 2024-11-14

Sweepstakes Script Code with Python to Set Weights of Sweepstakes Winners

Functional Description

The Sweepstakes system contains the following features:
1. Different weights can be set for different raffle winners
2. Start with high-value prizes first
3. Those who have already won will not participate in subsequent draws

coding

The first number indicates the winning number of a particular user and the second number indicates the winning weight of that user.
List.txtContent for:

1:20
2:10
3:8
4:6
5:6
6:3
7:2
8:1

Content for:

# Raffle Tools
# -*- coding:utf-8 -*-
from random import choice

def read_name_list(txt_name):
    with open(txt_name, 'r', encoding='utf-8') as f:
        txt_list = ()
    for i in range(len(txt_list)):
        txt_list[i] = txt_list[i].rstrip('\n')
    return txt_list

def lottery_draw(name_list, prize_box):
    full_number = []
    for name in name_list:
        number, weight = (':')
        full_number += [number] * int(weight)
    for prize in prize_box:
        luck_number = choice(full_number)
        print('【{}】The winning user number of:{}'.format(prize, luck_number))
        full_number = [i for i in full_number if i != luck_number]

if __name__ == '__main__':
    # Imported lists
    name_list = read_name_list('list.txt')
    # Setting up awards
    prize_box = ['Automobile', 'Computer', 'Cups', 'Banana', '50 cents red packet']
    # Raffle
    lottery_draw(name_list, prize_box)

The results of the run are as follows:

The winning user number of [Automobile] is: 5
The winning user number for [Computer] is: 1
The winning user number of [Mug] is: 3
The winning user number of [Banana] is: 2
The winning user number of [50 cents red packet] is: 6

validate (a theory)

Add a statistical functionstatisticsI'm going to change it.lottery_drawinputs and outputs, and then draw 1,000,000 draws to see if the results are the same as we envisioned with different users winning with different weights.
The full code is below:

# Raffle Tools
# -*- coding:utf-8 -*-
from random import choice
from collections import Counter

def read_name_list(txt_name):
    with open(txt_name, 'r', encoding='utf-8') as f:
        txt_list = ()
    for i in range(len(txt_list)):
        txt_list[i] = txt_list[i].rstrip('\n')
    return txt_list

def lottery_draw(name_list, prize_box, res):
    full_number = []
    for name in name_list:
        number, weight = (':')
        full_number += [number] * int(weight)
    for prize in prize_box:
        luck_number = choice(full_number)
        full_number = [i for i in full_number if i != luck_number]
        (prize+':'+luck_number)
    return res

def statistics(res):
    prize_cal = {'Automobile':[], 'Computer':[], 'Cups':[], 'Banana':[], '50 cents red packet':[]}
    for i in res:
        prize, luck_number = (':')
        prize_cal[prize].append(luck_number)
    for prize, number in prize_cal.items():
        print('【{}】Number of prizes won by each user of the:{}'.format(prize, Counter(number)))


if __name__ == '__main__':
    # Imported lists
    name_list = read_name_list('list.txt')
    # Setting up awards
    prize_box = ['Automobile', 'Computer', 'Cups', 'Banana', '50 cents red packet']
    # Validate the lottery system
    res = []
    for i in range(1000000):
        # Raffle
        res = lottery_draw(name_list, prize_box, res)
    # Statistical data
    statistics(res)

Results for:

The number of times each user of [Car] has won: Counter({'1': 356978, '2': 178116, '3': 143076, '5': 107189, '4': 107141, '6': 53638, '7': 35943, '8': 17919})
The number of times each user of [Computer] won the lottery: Counter({'1': 262385, '2': 192168, '3': 160879, '5': 125696, '4': 125308, '6': 65905, '7': 44822, '8': 22837})
The number of times each user of [Cup] has won: Counter({'2': 189568, '1': 180131, '3': 173171, '4': 144363, '5': 143846, '6': 82818, '7': 56424, '8': 29679})
The number of times each user of [Banana] won the lottery: Counter({'3': 173540, '2': 170010, '4': 162484, '5': 162387, '1': 111469, '6': 104839, '7': 75181, '8': 40090})
The number of times each user of [50 cents bonus] has won: Counter({'5': 173954, '4': 173772, '3': 157203, '6': 139308, '2': 136231, '7': 103678, '1': 58633, '8': 57221})

The following conclusions can be discerned:

  • As you can see by the first line, the percentage of occurrences between numbers is the same as the weights we set. For example1The number of occurrences is approximately equal to2Twice as many,4cap (a poem)5The number of occurrences is similar and both6Twice as many. It means that the probability of the winning number appearing is the same as the preset weights.
  • As can be seen by the lines that follow, for numbers with significant weights (e.g.1), even if the first prize is not won, the probability is that it will be won in the first few prizes, so the last prize in the1The number of occurrences is small because1It basically came up earlier. So the weighted numbers are prioritized to win prizes of high value, as envisioned.
  • For numbers with small weights, such as78The fact that the number of occurrences is the smallest regardless of the prize indicates that these numbers have a low probability of winning. The reason is also very simple, because the number of winning numbers is less than the number of prizes, so numbers with small weights have a low probability of winning, and it is very likely that you won't be able to draw them in all 5 prizes. If it is set to 8 prizes, then the end of the prize78The number of occurrences will be high, because at this point the number of winning numbers is equal to the number of prizes, and all the numbers win 100% of the prizes, so the numbers with small weights are basically small prizes with low values. For example, the following are the results of the experiment when there are 8 prizes:

The number of times each user of [Car] has won the lottery: Counter({'1': 357293, '2': 178396, '3': 142964, '4': 106969, '5': 106885, '6': 53787, '7': 35860, '8': 17846})
The number of times each user of [Computer] won the lottery: Counter({'1': 262439, '2': 191894, '3': 160317, '4': 125823, '5': 125372, '6': 66868, '7': 44747, '8': 22540})
The number of times each user of [Cup] has won: Counter({'2': 189590, '1': 179792, '3': 172228, '4': 144894, '5': 144557, '6': 82114, '7': 57173, '8': 29652})
The number of times each user of [Banana] won the lottery: Counter({'3': 173294, '2': 170689, '4': 162112, '5': 162085, '1': 111105, '6': 105137, '7': 75643, '8': 39935})
The number of times each user of [50 cents red packet] won: Counter({'5': 174098, '4': 173631, '3': 158224, '6': 138613, '2': 135805, '7': 103940, '1': 58826, '8': 56863})
The number of times each user of [Air] has won: Counter({'6': 201214, '7': 162491, '4': 156446, '5': 156151, '3': 117426, '8': 95013, '2': 87502, '1': 23757})
The number of times each user of [Spam] won the lottery: Counter({'7': 282600, '6': 225701, '8': 187694, '5': 99963, '4': 99165, '3': 60536, '2': 38211, '1': 6130})
Number of times each user of [Slap] has won: Counter({'8': 550457, '7': 237546, '6': 126566, '4': 30960, '5': 30889, '3': 15011, '2': 7913, '1': 658})

This article on Python can set the weight of the raffle draw script is introduced to this article, more related Python raffle draw script content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!