我正在尝试编写一个模拟彩票模拟器,作为一种思想锻炼和一些入门python的实践,其中每支球队在榜单上领先于他们的队伍将获得被选为第一的可能性的2倍。下面的代码可以工作(尽管我确信有一种更有效的编写方法),但是现在我想找出一种方法来找到每个团队的赔率,根据他们的赔率获得某个选秀位。相信有十二个!设置订单的方式,因此几乎无法手动计算。有没有一种方法可以在python中运行模拟(例如,进行一千万次),然后查看每个团队在混排列表中的某个位置所占的百分比是多少?
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]))
答案 0 :(得分:1)
我不会像这样进行抽样,而是使用离散分布来获得团队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
答案 1 :(得分:0)
这就是我想要的,但是正如另一条评论所述,它运行非常缓慢,我不会尝试1000万次,其运行速度足够慢。
from collections import Counter
for i in range(1,10000):
random.shuffle(total)
countList.append(total[0])
print Counter(countList)
在代码末尾添加for循环,并在顶部添加导入。
答案 2 :(得分:0)
这是一种方法(速度足够快,可以在15分钟内运行1M,所以对于1000万,您可能需要等待几个小时):
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())))