home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

Weighted Random Values In Python

May 2021

This is how to weight the return value of a seleciton

Code
import random

items = ['alfa', 'bravo', 'charlie', 'delta']
weights = [10, 20, 60, 10]

single_item = random.choices(items, weights, k=1)[0]
print(single_item)

weighted_list = random.choices(items, weights, k=5)
print(weighted_list)
Results
alfa
['charlie', 'charlie', 'charlie', 'charlie', 'bravo']
═══ § ═══