Last active
January 10, 2018 08:37
-
-
Save Dowwie/8409d871ddae913e44c61bc4d47ce1cc to your computer and use it in GitHub Desktop.
As of Python3.6, random.choices offers weighted choices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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