Weighted choice in python

Today, 25th June 2022, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putinโ€™s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

Works in 3.6+

random.choices([1,2], weights=[0.001, 0.8], k=1)[0]

Here is proof that makes 100k choices from [1,2] array and then counts numbers:

arr = [ch[0] for ch in [random.choices([1,2], weights=[0.001, 0.8], k=1) for _ in range(100000)]]
>>> arr.count(1), arr.count(2)
(114, 99886)
#python #statistics
Ivan Borshchov profile picture
Nov 01, 2017
by Ivan Borshchov
Did it help you?
Yes !

Best related