given a diictionary d, with all values are numbers, randomly pick an item and return it's key according to the percentage of its values
(d)
| 188 | #======================================================================== |
| 189 | |
| 190 | def randomPickDict(d): |
| 191 | ''' given a diictionary d, with all values are numbers, |
| 192 | randomly pick an item and return it's key according |
| 193 | to the percentage of its values''' |
| 194 | ks = d.keys() |
| 195 | vs = accumList(d.values(),1) |
| 196 | return ks[ findIndex( vs, random.random()) ] |
| 197 | |
| 198 | #======================================================================== |
| 199 |