Python Weighted Random Choice

April 28, 2019
use random.choices
import random

result = random.choices(['A', 'B', 'C'], [0.5, 0.3, 0.2])[0]

or

options = {
    'A': 0.5,
    'B': 0.3,
    'C': 0.2
}
result = random.choices(list(options.keys()), options.values())[0]
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.