I have a program where I\'m keeping track of the success of various things using collections.Counter
— each success of a thing increments the corr
The following will get a random item where the score is the weighting for how often to return that item.
import random
def get_random_item_weighted(scoreboard):
total_scoreboard_value = sum(scoreboard.values())
item_loc = random.random() * total_scoreboard_value
current_loc = 0
for item, score in scoreboard.items():
current_loc += score
if current_loc > item_loc:
return item
for instance, if there are 2 items:
item1 has a score 5
item2 has a score 10
item2 will be returned twice as often as item1