Advertisement
Bicorn

Is there a better way to do this?

May 12th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.28 KB | None | 0 0
  1. # src is a dictionary, each key's value is its relative probability as an arbitrary numeric value
  2. items = []
  3. weights = []
  4. s = 0.0
  5. for k, v in src.items():
  6.     items.append(k)
  7.     weights.append(v)
  8.     s += v
  9. weights = map(lambda x: x/s, weights)
  10. numpy.random.choice(items, p=weights)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement