i made the following for random selection of items based on the cost of said item, and it works, but not quite how i would like it to:
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)
the selection by probability occurs in the chance_calculate function of course, and that is the subject of my question. Basically, is there another more effective way to do this without having to create a list with more items per lower price?
EDIT: I have already seen that question, and I am looking for something a bit different, although i will check it out one more time.
Aucun commentaire:
Enregistrer un commentaire