Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # weighted_random.py
- # in this example, weighted['BB'] should be 20x-2x the random chance
- # *** {'C': ~100, 'AAA': ~100, 'BB': ~1800}
- from random import choice
- def weighted_random(z, tries=1):
- weighted = dict((k,0) for k in set(z))
- for _ in range(tries):
- r = choice(z)
- weighted[r] += 1
- return weighted
- #
- my_list = ['AAA'] + ['BB'] * 20 + ['C']
- for _ in range(5):
- results = weighted_random(my_list, 2000)
- print results
Add Comment
Please, Sign In to add comment