我根据所述项目的成本随机选择了项目,并且它有效,但不是我想要的那样:
def _selection():
choices = {x: weapon_dict[x][3] for x in weapon_dict}
choices.update({x: item_dict[x][3] for x in item_dict})
choices['cash'] = 100
choices['jackpot'] = 10000
choice = chance_calculate(choices)
cost = choices[choice]
return 'you found %s!' % choice
item_dict = {'health-potion': ['h', 10000, 2, 1000, 1], 'berserk': ['d', 2, 2, 1500, 40], 'luck-potion':['l', 2, 1, 100, 15], 'revive':['h',1,1,1000,1]}
weapon_dict = {'itemd': ['h', 50, 150, 25, 1], 'itema': ['h', 23, 5, 1500, 15], 'randomitem2':['h', 6969, 2, 2500, 1], 'lazer-gun':['h', 2000, 45, 675, 1], 'a34':['a', 500, 10, 450, 1], 'randomitem':['h',25000, 3, 1000000, 2]}
def chance_calculate(choices):
lista = sorted(choices, key=choices.get)
listb = sorted(choices.values())[::-1]
choices = dict(zip(lista, listb))
n = []
for x in choices:
choices[x] = choices[x]/100
if choices[x] < 1: choices[x] = 1
choices[x] = round(choices[x])
if choices[x] > 300: choices[x] = 300
if x == 'cash': choices[x] = 250
for y in range(choices[x]):
n.append(x)
print(n)
return random.choice(n)
概率选择当然发生在chance_calculate函数中,这是我的问题的主题。基本上,是否有另一种更有效的方法来做到这一点,而不必创建一个每个更低价格更多项目的列表?
编辑:我已经看过这个问题了,我正在寻找一些不同的东西,虽然我会再次检查一下。