L
L
leham12021-04-19 16:21:04
Python
leham1, 2021-04-19 16:21:04

How to make a random selection in python, but with a certain probability?

Hello.
There is a list: list = [q1, q2, q3]
You need to choose one value from this list (of type random.choice(list), but with different probabilities for selection. For example, with probabilities:
q1 = 0.2
q2 = 0.3
q3 = 0.5
Is it possible, and if so, how can it be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-04-19
@leham1

There is also random.choices. The weights parameter specifies the weights of each value.

values = [q1, q2, q3]
data = random.choices(values, weights=[0.2, 0.3, 0.5], k=1000)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question