我正在尝试编写一个模拟彩票模拟器,作为一个思想练习和一些介绍性的 python 练习,其中每个团队将有 2 倍的几率获得第一个选择,因为他们之前的团队在积分榜上。
import random
from time import sleep
first = [1*['team 1']]
second = [2*['team 2']]
third = [4*['team 3']]
fourth = [8*['team 4']]
fifth = [16*['team 5']]
sixth = [32*['team 6']]
seventh = [64*['team 7']]
eighth = [128*['team 8']]
ninth = [256*['team 9']]
tenth = [512*['team 10']]
eleventh = [1024*['team 11']]
twelfth = [2048*['team 12']]
total = []
for i in first:
for x in i:
total.append(x)
for i in second:
for x in i:
total.append(x)
for i in third:
for x in i:
total.append(x)
for i in fourth:
for x in i:
total.append(x)
for i in fifth:
for x in i:
total.append(x)
for i in sixth:
for x in i:
total.append(x)
for i in seventh:
for x in i:
total.append(x)
for i in eighth:
for x in i:
total.append(x)
for i in ninth:
for x in i:
total.append(x)
for i in tenth:
for x in i:
total.append(x)
for i in eleventh:
for x in i:
total.append(x)
for i in twelfth:
for x in i:
total.append(x)
random.shuffle(total)
order = []
for i in total:
if i not in order:
order.append(i)
print('the twelfth pick goes to {}'.format(order[11]))
sleep(1)
print('the eleventh pick goes to {}'.format(order[10]))
sleep(1)
print('the tenth pick goes to {}'.format(order[9]))
sleep(1)
print('the ninth pick goes to {}'.format(order[8]))
sleep(1)
print('the eighth pick goes to {}'.format(order[7]))
sleep(1)
print('the seventh pick goes to {}'.format(order[6]))
sleep(2)
print('the sixth pick goes to {}'.format(order[5]))
sleep(2)
print('the fifth pick goes to {}'.format(order[4]))
sleep(2)
print('the fourth pick goes to {}'.format(order[3]))
sleep(3)
print('the third pick goes to {}'.format(order[2]))
sleep(3)
print('the second pick goes to {}'.format(order[1]))
sleep(3)
print('the first pick goes to {}'.format(order[0]))
而不是像这样做你的采样,我会使用一个离散分布来获得团队 i 的概率,并使用 random.choices 进行采样。我们通过丢弃该团队的所有票来更新采样后的分布(因为它不能再次出现)。
from random import choices
ticket_amounts = [2**i for i in range(12)]
teams = [ i + 1 for i in range(12)]
for i in range(12):
probabilities = [count/sum(ticket_amounts) for count in ticket_amounts]
team_picked = choices(teams, probabilities)[0]
print("team picked is{}".format(team_picked))
# update probabilities by discarding all the tickets
# belonging to picked team
ticket_amounts[team_picked-1] = 0
这是你想要的,我认为,但正如其他评论说,它运行非常缓慢,我不会尝试 10 万次,它足够慢。
from collections import Counter
for i in range(1,10000):
random.shuffle(total)
countList.append(total[0])
print Counter(countList)
将 for 循环添加到代码的末尾,并在顶部添加导入。
这是一种方法(它足够快,可以在大约 15 分钟内运行 1M 时间,因此对于 10 百万,您可能需要等待几个小时):
import numpy as np
from collections import Counter
n_teams = 12
n_trials = int(1e4)
probs = [ 2**i for i in range(0,n_teams) ]
probs = [ prob_i / sum(probs) for prob_i in probs ]
lottery_results = np.zeros([n_trials, n_teams],dtype=np.int8) # to store the positions at each lottery
for i in range(n_trials):
lottery_results[i,:] = np.random.choice(n_teams, n_teams, replace=False, p=probs)
for i in range(n_teams):
positions = Counter(lottery_results[:,i])
print("Team {}".format(i), dict(sorted(positions.items())))
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(9条)