我有一个加权随机状态函数,它接受有限个相对权重,并根据伪随机选择输出一个整数。但是,我想使函数可扩展以处理n个状态/权重。重写这个函数的优雅方法是什么?它可以将*args作为输入?你知道吗
编辑:由于这些权重是相互关联的,所以对于我来说,棘手的部分是思考使elif逻辑可扩展的最佳方法。你知道吗
def weighted_random(weight1, weight2, weight3, weight4, weight5, weight6, weight7, weight8):
totalWeight = weight1 + weight2 + weight3 + weight4 + weight5 + weight6 + weight7 + weight8
randomInt = random.randint(1, totalWeight)
if randomInt <= weight1:
return 0
elif randomInt > weight1 and randomInt <= (weight1 + weight2):
return 1
elif randomInt > (weight1 + weight2) and randomInt <= (weight1 + weight2 + weight3):
return 2
elif randomInt > (weight1 + weight2 + weight3) and randomInt <= (weight1 + weight2 + weight3 + weight4):
return 3
elif randomInt > (weight1 + weight2 + weight3 + weight4) and randomInt <= (weight1 + weight2 + weight3 + weight4 + weight5):
return 4
elif randomInt > (weight1 + weight2 + weight3 + weight4 + weight5) and randomInt <= (weight1 + weight2 + weight3 + weight4 + weight5 + weight6):
return 5
elif randomInt > (weight1 + weight2 + weight3 + weight4 + weight5 + weight6) and randomInt <= (weight1 + weight2 + weight3 + weight4 + weight5 + weight6 + weight7):
return 6
elif randomInt > (weight1 + weight2 + weight3 + weight4 + weight5 + weight6 + weight7):
return 7
else:
return("error")
可以使用
*args
以列表形式获取所有参数。然后您可以遍历它们,以找到randomInt
命中了哪一个。你知道吗如果可读性太强或O(n)太快,请尝试以下方法:
如果您试图创建一个使用累积权重的函数,那么可以按照以下几行进行操作(假设Python 3.6+):
因此,假设您希望
[0,1,2,3,4,5]
的权重为[50,5,5,5,15,20]
(因为它们加起来等于100,可以看作百分比),您可以执行以下操作:接近假定的百分比。你知道吗
如果要在Python 3.6+以外的Python上执行此操作,可以生成列表累积和的元组,然后使用
bisect
获取随机数在该元组列表中的位置索引:测试:
与最初发布的功能相比:
相关问题 更多 >
编程相关推荐