A lucky draw algorithm

A lottery algorithm is used to draw a lottery among the eight objects in the following array. According to the probability of winning the lottery, the probability of winning the lottery is returned, which can be added up to 100%

according to the probability of winning the lottery. The probability of winning the lottery can be br

.
[
    {"a", 20.00%},
    {"b", 20.00%},
    {"c", 20.00%},
    {"d", 10.00%},
    {"e", 10.00%},
    {"f", 10.00%},
    {"g", 10.00%},
    {"h", 0.05%},
]
Php
Mar.13,2021

you can assume that the total probability is 1-100a, 1-20b, 21-40, and so on. For example, 78 is the simplest
that can be processed, such as random to 78, once at random, if in the middle, once at random, set a maximum random number of times, for example, 5 times. If all 5 times are random, 78 is regarded as winning the lottery. This maximum random number of times is set according to your actual situation


the total probability of winning the lottery is 100, then you can regard it as a straight line, according to the probability of winning the lottery, a = 1-20, 21-40, 41-60, 61-70, 71-80, respectively. So random a number from 1 to 100, depending on which interval the number is in.


Program idea:
1. The understanding of the probability of winning the lottery: the greater the probability, the greater the probability of being picked. Assuming that the total number of letters a murh (there can be repetitions) is S, and the number of letters an is A, then the probability of a being picked is: P = An and S
2. The alphabet object and probability given in the title. Assuming that the total number of letters a murh is 2000, then according to the probability, a should be 400, b 400, c 400, d 200, e 200, f 200, g 200, and h 1.
3. Think of all the letters as balls, put them in one pocket and pick them at random. The program language describes that all the letters are stored in an array, a random number at a time as a subscript, and the letters obtained according to the subscript are the result of extraction.
simple programming (python)

-sharp usr/bin/python
-sharp -*- coding=utf-8 -*-
-sharp 
data = [
    ["a", 20.00%],
    ["b", 20.00%],
    ["c", 20.00%],
    ["d", 10.00%],
    ["e", 10.00%],
    ["f", 10.00%],
    ["g", 10.00%],
    ["h", 0.05%],
]
-sharp h12000
all = 2000
arr = []
for i in range(len(data)):
    letter = data[i][0]
    num = data[i][1]*all
    for j in range*(num):
        arr.append(letter)
-sharp
index = random.randint(0,len(arr))
-sharp
result = arr[index]

Menu