Skip to main content
 首页 » 编程设计

python 随机数

2025年05月04日9arxive

我想知道是否有人可以为我澄清这一行。

Create a function die(x) which rolls a die x times keeping track of how many times each face comes up and returns a 1X6 array containing these numbers.

当它说 1X6 数组时,我不确定这是什么意思?我正在使用 numpy 的 randint 函数,所以输出已经是一个数组(或列表)我不确定。

谢谢

请您参考如下方法:

def die(x): 
    return np.bincount(np.random.random_integers(0, 5, size=x)) 

np.random.random_integers(0,5,size=x) 掷骰子 x 次(面由 0 中的数字表示> 到 5,包括)。

np.bincount() 返回数组中每个值的出现次数,即每个面出现的次数。

例子

>>> a = np.random.random_integers(0, 5, size=10) 
>>> a 
array([3, 5, 0, 5, 0, 5, 5, 1, 3, 0]) 
>>> np.bincount(a) 
array([3, 1, 0, 2, 0, 4])