A simple library for returning a weighted random choice from a list
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore
README.md
test.py
weighted_random.py

README.md

weighted_random

A simple library for returning a weighted random choice from a list:

from weighted_random import weighted_choice

# Alex is 10 times more likely to win than Steve
winner = weighted_choice(['Alex', 'Steve'], [10, 1])

A good way to demonstrate this is by running it thousands of times:

winners = {'Alex': 0, 'Steve': 0}
for i in range(10000):
    winner = weighted_choice(['Alex', 'Steve'], [10, 1])
    winners[winner] += 1

print winners
# {'Steve': 960, 'Alex': 9040}