The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Weighted Random Values In Python

TODO: Pull subtitle into page object

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']

The `k=1` says to return only one item in the list and the `[0]`` returns it directly

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Weighted Random Values In Python

-- p

This is how to weight the return value of a seleciton

-- code
-- python

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']

-- /results

The `k=1` says to return only one item in the 
list and the `[0]`` returns it directly


-- categories
-- Python 

-- metadata
-- date: 2021-05-18 10:44:45
-- id: 01f5zyzb
-- status: published
-- type: post
-- site: aws