Problem: two billboards and five advertisers design an algorithm. The number of advertisements of five advertisers in a period of time is 1:2:3:4:5. Note that two billboards cannot broadcast the same advertisement at the same time.
Considering that there is only one billboard, you only need to select the corresponding advertisement according to the following probability
P
=
[
1
15
,
2
15
,
3
15
,
4
15
,
5
15
]
P=[\frac{1}{15},\ \frac{2}{15},\ \frac{3}{15},\ \frac{4}{15},\ \frac{5}{15}]
P=[151, 152, 153, 154, 155]
The specific implementation is through the roulette algorithm, that is, to generate a random number on the [0,1) interval
r
r
r. According to
r
r
Select the corresponding advertisement at the position where r falls in the cumulative probability interval. The cumulative probability is
P
c
u
m
s
u
m
=
[
1
15
,
3
15
,
6
15
,
10
15
,
15
15
]
P_{cumsum}=[\frac{1}{15} ,\ \frac{3}{15} ,\ \frac{6}{15} ,\ \frac{10}{15},\ \frac{15}{15}]
Pcumsum=[151, 153, 156, 1510, 1515]
if
r
≤
1
15
r\leq\frac{1}{15}
If r ≤ 151, select the first advertisement; if
1
15
<
r
≤
3
15
\frac{1}{15}<r\leq \frac{3}{15}
151 < R ≤ 153, select the second advertisement; And so on
The corresponding implementation code is
from random import random N = 1000000 cnt_dict = {'1':0, '2':0, '3':0, '4':0, '5':0} for i in range(N): r = random() if r<1/15: cnt_dict['1'] += 1 elif r<3/15: cnt_dict['2'] += 1 elif r<6/15: cnt_dict['3'] += 1 elif r<10/15: cnt_dict['4'] += 1 else: cnt_dict['5'] += 1 prob = [float('{:.3f}'.format(ele/sum(cnt_dict.values()))) for ele in cnt_dict.values()] prob_required = [float('{:.3f}'.format(i/15)) for i in range(1,6)] print(prob_required) print(prob)
Now consider the case that there are two billboards. If there is no restriction that two billboards cannot play the same advertisement at the same time, only two billboards need to operate independently of each other. However, the topic requires that two billboards cannot play the same advertisement at the same time. Consider allowing one billboard to select an advertisement to play according to the above method, and then select one of the remaining advertisements to play on the other billboard according to a certain probability. One implementation method is that if advertisements 1 and 4 are selected, another billboard plays advertisement 5; If 5 is selected, the
2
5
\frac{2}{5}
52. The probability of selecting advertisement 2 to play on another billboard to
3
5
\frac{3}{5}
53. The probability of selecting advertisement 3 to play on another billboard; If advertisement 2 is selected, equally likely choices 1 and 4 are played on another billboard. If advertisement 3 is selected, advertisement 4 is played on another billboard.
The specific implementation is as follows
from random import random N = 1000000 cnt_dict = {'1':0, '2':0, '3':0, '4':0, '5':0} for i in range(N): r = random() if r<1/15: cnt_dict['1'] += 1 cnt_dict['5'] += 1 elif r<3/15: cnt_dict['2'] += 1 if random()<1/2: cnt_dict['1'] += 1 else: cnt_dict['4'] += 1 elif r<6/15: cnt_dict['3'] += 1 cnt_dict['4'] += 1 elif r<10/15: cnt_dict['4'] += 1 cnt_dict['5'] += 1 else: cnt_dict['5'] += 1 if random()<2/5: cnt_dict['2'] += 1 else: cnt_dict['3'] += 1 prob = [float('{:.3f}'.format(ele/sum(cnt_dict.values()))) for ele in cnt_dict.values()] prob_required = [float('{:.3f}'.format(i/15)) for i in range(1,6)] print(prob_required) print(prob)