Skip to content

Instantly share code, notes, and snippets.

@yerttle
Last active July 25, 2023 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yerttle/dbdb879ca3f55acee482165b02b1d553 to your computer and use it in GitHub Desktop.
Save yerttle/dbdb879ca3f55acee482165b02b1d553 to your computer and use it in GitHub Desktop.
import random
hobbies = ['a', 'b', 'c']
hobbies_wieghts = [70, 20, 10]
def choice_hobby():
# list enumerate here wraps the arrary in a list so choices returns the indexes
c = random.choices(list(enumerate(hobbies)), weights=hobbies_wieghts, k=1)
# For the first item in the list get the first item in the inner list
arrary_index = c[0][0]
# For the first item in the list get the second item in the inner list
array_value = c[0][1]
# Set the wieght for this item to 0 so it is never choosen again
hobbies_wieghts[arrary_index] = 0
# Return the value
return array_value
for i in range(len(hobbies)):
print(choice_hobby())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment