Can the lower the number in Python, the higher the probability of being picked?

Asked 2 years ago, Updated 2 years ago, 16 views

We know that if you give weight a probability in the random.choice command, it will be drawn according to that probability. But on the other hand, there's a necessary situation. For example, the lower the probability (number), the higher the probability of being chosen The higher the probability (number), the lower the probability of being picked, or I want to write it either way Is there any way?

python

2022-09-22 18:52

1 Answers

>>> samples = random.choices([1,2,3,4], weights=[90, 5, 3, 2], k=10000)
>>> counter = collections.Counter(samples)
>>> counter
Counter({1: 9047, 2: 483, 3: 292, 4: 178})

You just have to give weights well. It was roughly 90%, 5%, 3%, and 2%.

You just need to roughly make the weights formula and put it in. You can give me 1/n.


2022-09-22 18:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.