【问题标题】:Dice outcome evaluation based on bias基于偏差的骰子结果评估
【发布时间】: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


【解决方案1】:

真的吗?

    return random.choice(np.arange(N),p=bias)

编辑
我误解了要求。您希望它返回 N 次滚动的结果。

def roll(N,bias):
    return (random.choice(range(len(bias)),p=bias)+1 for _ in range(N))

【讨论】:

  • 你有什么建议?
  • 就是这样。这就是修复。你有range,当实际的numpy函数被调用arange。
  • 很酷,但是在 return 语句中说 'a' 和 'p' 必须具有相同的值
  • 您可以使用list(roll(N,bias))。或者,如果您愿意,可以将其作为函数的返回值,因此它直接返回一个列表。
  • 没错,他们没有。如果你真的需要 5:4:3:3:2:5 的比例,那么你必须改变你的分数加起来为 1。 [.227, .182, .136, .136, .092, .227 ]
猜你喜欢
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 2011-07-17
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多