【发布时间】:2021-03-13 05:46:58
【问题描述】:
我正在尝试在偏差条件下确定掷骰子,在实现其代码期间,我遇到了这个错误(代码和错误):
from numpy import random
def roll(N,bias):
'''will return the dice outcome'''
return random.choice(np.range(N),p=bias)
>>N= 50
>>bias= (0.25, 0.2, 0.15, 0.15, 0.1, 0.25)
>>roll(N,bias) # returns 50 outcomes of dice (1,3,4,5...)
Error:---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-105-ac839882958b> in <module>
1 N= 50
2 bias= (0.25, 0.2, 0.15, 0.15, 0.1, 0.25)
----> 3 roll(N,bias)
<ipython-input-104-d4d7cbf35d08> in roll(N, bias)
2 def roll(N,bias):
3 '''will return the dice outcome'''
----> 4 return random.choice(np.range(N),p=bias)
D:\anaconda\lib\site-packages\numpy\__init__.py in __getattr__(attr)
213 else:
214 raise AttributeError("module {!r} has no attribute "
--> 215 "{!r}".format(__name__, attr))
216
217 def __dir__():
AttributeError: module 'numpy' has no attribute 'range'
目标是返回一个基于偏差的掷骰值列表。 你能告诉我这里的代码有什么问题吗?
【问题讨论】:
-
我猜你可以粘贴它。
标签: python function numpy random return