Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. import random
  2.  
  3. def weighted_choice(choices):
  4. total = sum(w for c, w in choices)
  5. r = random.uniform(0, total)
  6. upto = 0
  7. for c, w in choices:
  8. if upto + w >= r:
  9. break
  10. upto += w
  11. if c == None:
  12. weighted_choice(choices)
  13. else:
  14. return c
  15.  
  16. print(weighted_choice([('Mcdonalds',100.0),('Max\'s Restaurant',40.0),('Jollibee',3.0)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement