[英]Python function that simulates rolling dice based on input parameters 1) number of dice 2) Number of rolls
I have been looking on stack and spend several hours browsing to try and solve this.我一直在查看堆栈并花了几个小时浏览以尝试解决这个问题。 Task is:
任务是:
Write a Python function called dicerolls that simulates rolling dice.编写一个名为 dicerolls 的 Python function 来模拟掷骰子。 Your function should take two parameters: the number of dice k and the number of times to roll the dice n.
你的 function 应该有两个参数:骰子的数量 k 和掷骰子的次数 n。 The function should simulate randomly rolling k dice n times, keeping track of each total face value.
function 应该模拟随机滚动 k 骰子 n 次,跟踪每个总面值。 It should then return a dictionary with the number of times each possible total face value occurred.
然后它应该返回一个字典,其中包含每个可能的总面值出现的次数。 So, calling the function as diceroll(k=2, n=1000) should return a dictionary like: {2:19,3:50,4:82,5:112,6:135,7:174,8:133,9:114,10:75,11:70,12:36}
因此,调用 function 作为 diceroll(k=2, n=1000) 应该返回一个字典,如:{2:19,3:50,4:82,5:112,6:135,7:174,8:133 ,9:114,10:75,11:70,12:36}
So far I have managed to define the dice function but where I am struggling is to add the k(number of rolls) into the dicerolls function.到目前为止,我已经设法定义了骰子 function,但我正在努力将 k(掷骰数)添加到骰子 function 中。 What I have so far:
到目前为止我所拥有的:
from numpy import random
def dice():
return random.randint(1, 7)
def diceroll(number_of_times):
counter = {n : 0 for n in range(2, 13)}
for i in range(number_of_times):
first_dice = dice()
second_dice = dice()
total = first_dice + second_dice
counter[total] += 1
return counter
diceroll(1000)
Output: {2: 19, 3: 49, 4: 96, 5: 112, 6: 150, 7: 171, 8: 151, 9: 90, 10: 89, 11: 47, 12: 26} Output:{2:19、3:49、4:96、5:112、6:150、7:171、8:151、9:90、10:89、11:47、12:26}
Any suggestions are appreciated.任何建议表示赞赏。
Edited code after answer回答后编辑代码
import random
def diceroll(k, n, dice_sides=6):
# prepare dictionary with zero values for all possible results
counter = {n : 0 for n in range(k, k*dice_sides + 1)}
# roll all the dice
for i in range(n):
dice_sum = sum(random.choices(range(1, dice_sides + 1), k = k))
counter[dice_sum] += 1
return counter
diceroll(k=2, n=1000)
Output: {2: 20, 3: 49, 4: 91, 5: 116, 6: 140, 7: 138, 8: 173, 9: 112, 10: 72, 11: 65, 12: 24} Output:{2:20、3:49、4:91、5:116、6:140、7:138、8:173、9:112、10:72、11:65、12:24}
You could use collectors.counter to keep track of the rolls.您可以使用 collectors.counter 来跟踪卷。
Also, it might be down to preference, but there is no reason to import numpy for something so simple like random.此外,这可能取决于偏好,但没有理由为像随机这样简单的东西导入 numpy。
In [1]: import random
In [2]: from collections import Counter
In [3]: def dice():
...: return random.randint(1,7)
...:
In [4]: def dice_roll(number_of_times):
...: counter = Counter()
...: for i in range(number_of_times):
...: first_dice = dice()
...: second_dice = dice()
...: total = first_dice + second_dice
...: counter[total] += 1
...: return counter
...:
In [5]: def multiple_rolls(k, number_of_times):
...: final_counter = Counter()
...: for i in range(k):
...: final_counter.update(dice_roll(number_of_times))
...: return final_counter
...:
In [6]: multiple_rolls(2, 1000)
Out[6]:
Counter({9: 247,
5: 170,
10: 198,
6: 196,
8: 251,
4: 123,
12: 102,
7: 249,
14: 44,
2: 44,
11: 184,
3: 105,
13: 87})
You can leverage the random function that provides multiple rolls of the same funciton at once: random.choices(iterable, k=number of results)
.您可以利用随机 function 一次提供多个相同功能的滚动:
random.choices(iterable, k=number of results)
。 This is faster then rolling 1 dice multiple times and add the values up.这比多次滚动 1 个骰子并将值相加要快。
You would need to change your code to:您需要将代码更改为:
import random
def diceroll(number_of_dices, number_of_times, dice_sides=6):
# prepare dictionary with zero values for all possible results
counter = {n : 0 for n in range(number_of_dices, number_of_dices*dice_sides + 1)}
# roll all the dice
for i in range(number_of_times):
dice_sum = sum(random.choices(range(1, dice_sides + 1), k = number_of_dices))
counter[dice_sum] += 1
return counter
print(diceroll(3, 100))
Output: Output:
{ 3: 0, 4: 1, 5: 1, 6: 7, 7: 10, 8: 10, 9: 16, 10: 10,
11: 19, 12: 8, 13: 8, 14: 3, 15: 4, 16: 2, 17: 1, 18: 0}
If you really want to use numpy
, this will be faster, but only noticeably faster if your have large numbers of rolls and dice:如果你真的想使用
numpy
,这会更快,但如果你有大量的掷骰子和骰子,这会明显更快:
def diceroll(k, n, dice_sides=6):
rolls = np.random.randint(dice_sides, size = (k, n)) + 1
counter = {k:v for k, v in zip(*np.unique(rolls.sum(1), return_counts = True))}
return counter
diceroll(1000, 2)
Out[]:
{2: 31,
3: 49,
4: 105,
5: 120,
6: 136,
7: 163,
8: 152,
9: 109,
10: 70,
11: 40,
12: 25}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.