Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def weighted_choice(choices):
- total = sum(w for c, w in choices)
- r = random.uniform(0, total)
- upto = 0
- for c, w in choices:
- if upto + w >= r:
- break
- upto += w
- if c == None:
- weighted_choice(choices)
- else:
- return c
- print(weighted_choice([('Mcdonalds',100.0),('Max\'s Restaurant',40.0),('Jollibee',3.0)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement