我正在尝试模拟6张面部骰子卷并存储结果。即。
后来,我计算每次迭代中每个数字出现的次数。对于较小的迭代,它可以很好地运行,但是当我为100,000次大数选择模拟时,代码需要永远模拟。
如何使此代码高效?我将不胜感激任何建议。
import random
from collections import defaultdict
from collections import Counter
# How many time we want to roll a dice
number_of_rolls = 100000
# Define Blank Dictionary to store output of Dice rolls
roll = defaultdict(list)
# Roll the dice and store output
for i in range(number_of_rolls):
for j in range(i):
roll[i].append(random.sample(dice,1)[0])
# Count occurance of each number from dice roll
obv = defaultdict(dict)
for i in range(len(roll)):
obv[i]=dict(Counter(roll[i]))
谢谢, 拉马