Weight each item and perform n consecutive gachas
The following items are prepared as parameters
#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
#Number of lottery
times = 11
import random
def gacha(item_dic, times):
total_weight = 0
for value in item_dic.values():
total_weight += value
results = []
for i in range(times):
results.append(lottery(item_dic,total_weight))
return results
def lottery(item_dic, total_weight):
score = random.randint(1,total_weight)
range_max = 0
for item_key, weight in item_dic.items():
range_max += weight
if score <= range_max:
return item_key
item_list = gacha(item_dic, times)
I think there is a more beautiful way to write it, but for the time being.
Recommended Posts