I have a list with around 40 strings and want to assign every item a weight / percentage. During runtime I now want a randomizer to pick an item from the list according to its percentage. Meaning that over a large enough sample size the number of times this item gets picked would correspond to the assigned percentage. A problem that I'm facing is that in the future I might want to extend the list and would then have to assign a new percentage to other items. What would be the best way to save this list and assign weights to individual items?
I can think of some ways to implement this but they are all rather quick & dirty so I was hoping somebody has a design pattern in mind. I'm working in Python but since this is conceptual I'm not really fishing for explicit examples.
Thank you so much for your help.
Check out this page: Weighted random generation in Python
Edit: See this also (on SO): A weighted version of random.choice
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With