0

I have a list ['a','b','c','d'] and I want to randomly select

'a' = 75 %

'b' = 15 %

'c' = 5 %

'd' = 5 %

Times how do I achieve this in python

thanks in advance.

sachin
  • 27
  • 5
  • 2
    All your choices are 'a' – alani Sep 07 '20 at 13:15
  • Anyway, there is an (off-site) answer at https://www.kite.com/python/answers/how-to-get-a-weighted-random-choice-in-python . I guess there is probably also a duplicate on SO but I haven't looked hard for it. – alani Sep 07 '20 at 13:16
  • its simple simply use the `randint()` function in your script for random percentage values – psycho developer Sep 07 '20 at 13:17
  • https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice – d125q Sep 07 '20 at 13:17
  • Yes okay, looks like `random.choices` supports `weights` - so just use that with `k=1` and then it's not dependent on numpy (`random.choice` doesn't support a weights parameter) – alani Sep 07 '20 at 13:18
  • So anyway... `random.choices("abcd", weights=[75,15,5,5], k=1)[0]`. But given the dup, I'm not going to post it as an answer. – alani Sep 07 '20 at 13:20
  • @alani thank you alani :) – sachin Sep 07 '20 at 17:45

0 Answers0