Free video project · Intermediate
Secret Santa picker 🎅
Pulling names from a hat goes wrong 63% of the time with six people. This picker never lets anyone draw themselves and keeps couples apart.
The video is out Friday 4 December at 6pm.
The code from the video
# Secret Santa picker
import random
random.seed(25)
PEOPLE = ["Amy", "Ben", "Chloe", "Dev", "Ellie", "Femi"]
fails = 0
for _ in range(10000):
hat = PEOPLE.copy()
random.shuffle(hat)
for giver, receiver in zip(PEOPLE, hat):
if giver == receiver:
fails += 1
break
print(f"A hat draw goes wrong {fails / 10000:.0%} of the time")
COUPLES = [("Amy", "Ben"), ("Dev", "Ellie")]
def allowed(giver, receiver):
if giver == receiver:
return False
if (giver, receiver) in COUPLES or (receiver, giver) in COUPLES:
return False
return True
def draw():
tries = 0
while True:
tries += 1
hat = PEOPLE.copy()
random.shuffle(hat)
pairs = list(zip(PEOPLE, hat))
good = True
for giver, receiver in pairs:
if not allowed(giver, receiver):
good = False
if good:
return pairs, tries
pairs, tries = draw()
print("Found a fair draw after", tries, "tries")
for giver, receiver in pairs:
print(f"{giver:<6} buys for {receiver}")
Run it in your browser with nothing to install, right here on this page.
Make it your own
- Add a rule that nobody gets the same person as last year.
- Set a budget and print it next to each pairing.
- Make one big loop so everyone buys for the next person, with no small circles.
Skills: random, zip, while loops, functions