Choosing elements from python list based on probability
python
Solution
An easy algorithm for weighted selection is:
Assign each name its relative probability, such that the sum of all probabilities is 1. This relative value is called "weight".
Select a random number between 0 and 1
Walk the list, substracting the weight of each item from your number as you go
When you go to 0 or below, pick the current item.
Problem
I'm creating a python script that randomly picks 1000 names from the list of male first names located here: http://www.census.gov/genealogy/www/data/1990surnames/names_files.html That works all fine and dandy, but I would like it so that the names were selected based on the probability column provided by the census text files (second column). I've been trying to wrap my head around this for the past few hours, but I haven't made any real progress, even looking for other answers. Can anybody help me out or point me in the right direction? Thanks in advance :)