Skip to content

Instantly share code, notes, and snippets.

@wohlben
Forked from Dowwie/wtd_choices.py
Created January 10, 2018 08:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wohlben/464840d63d1b683bc7f4f3250e0dcf1a to your computer and use it in GitHub Desktop.
Save wohlben/464840d63d1b683bc7f4f3250e0dcf1a to your computer and use it in GitHub Desktop.
As of Python3.6, random.choices offers weighted choices
age_groups = [lambda: random.randint(18,25),
lambda: random.randint(25,40),
lambda: random.randint(40,90)]
age_group_probs = [.5, .3, .1]
def get_choices():
pop_choices = random.choices(age_groups, weights=age_group_probs, k=10)
population = list(map(lambda x: x(), pop_choices))
return population
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment