Secret Santa Draw: Run a Gift Exchange From a Plain-Text File
You can run a Secret Santa draw with a Markdown file and a ten-line script: list the names, note who can't draw whom, shuffle, and send each person only their own result. No signup, no email addresses handed to a website.
In a Secret Santa exchange, members of a group are randomly assigned a person to whom they give a gift, and the giver may stay secret. The only real work is a fair draw nobody can peek at. Below you'll find the file layout, exclusions, the one way a plain shuffle goes wrong, the script, and how to hand out results.
Set up the participants file
Start with a Markdown task list for the names. In GitHub Flavored Markdown, - [ ] is an unchecked item and - [x] is a checked one. Use that as a sign-up sheet: check a name once the person confirms. The script only reads checked names.
Add an exclusions table below it. A table needs a header row and a delimiter row with the same number of cells, or it won't be recognized. Here is a whole sample file, shown inside a fenced code block:
Budget: 25 per gift (example, set your own)
## Names
- [x] Ana
- [x] Ben
- [x] Chloe
- [x] Dev
- [ ] Elif
## Exclusions
| Giver | Cannot draw |
| --- | --- |
| Ana | Ben |
| Ben | Ana |Elif hasn't confirmed, so she's left out for now. If you already keep a holiday gift list in Markdown, the names can live in the same file.
Add exclusions and a budget
Exclusions cover the usual cases: partners who already shop for each other, or last year's match. Each row says one giver can't draw one receiver. Write both directions if the rule is mutual, as with Ana and Ben above.
Keep the budget as a plain line at the top so everyone sees the same number. Spending limits and exclusion rules are your group's call, not a standard, so decide them before the draw. To follow what everyone actually spends afterwards, track gift spending in a table.
Why a plain shuffle can fail
"Nobody draws themselves" has a name: a derangement is a permutation in which no element appears in its original position. A random shuffle of your names might not be one.
The odds are better than you'd fear. For a large group, the probability that a random shuffle is a derangement approaches 1/e, about 36.8%, and it gets there quickly. For three people, 2 of the 6 possible arrangements qualify. For four, it's 9 of 24. Shuffling and trying again is fast.
The Fisher–Yates shuffle makes every ordering equally likely, but it doesn't rule out self-matches. Skip homemade shortcuts. Swapping each item with a random one from the whole list is biased, and so is sorting with a random comparator. Your exclusions shrink the pool of valid draws even more, which is why the script below caps its retries.
Run the draw with a short script
Save this as draw.py. It reads checked names and the exclusions table, shuffles until the draw is valid, and writes one small file per giver:
import random, re, sys
from pathlib import Path
lines = Path(sys.argv[1]).read_text().splitlines()
names = [m.group(1).strip() for l in lines
if (m := re.match(r"\s*- \[[xX]\] (.+)", l))]
banned = {}
for l in lines:
cells = [c.strip() for c in l.strip().strip("|").split("|")]
if l.startswith("|") and len(cells) == 2 and cells[0] in names:
banned.setdefault(cells[0], set()).add(cells[1])
for _ in range(1000):
receivers = names[:]
random.shuffle(receivers)
if all(g != r and r not in banned.get(g, ())
for g, r in zip(names, receivers)):
break
else:
sys.exit("No valid draw found. Loosen the exclusions.")
out = Path("draw")
out.mkdir(exist_ok=True)
for giver, receiver in zip(names, receivers):
(out / f"{giver}.txt").write_text(f"{giver}, you are buying for {receiver}.\n")Run it with python3 draw.py secret-santa.md. It never prints the pairings to the screen, so you can stay surprised too.
There's a second route that skips retries when you have no exclusions. Sattolo's variant of Fisher–Yates picks the swap index strictly below the current position, and the result always consists of a single cycle. Nobody can draw themselves. Replace the random.shuffle(receivers) line with:
for i in range(len(receivers) - 1, 0, -1):
j = random.randrange(i)
receivers[i], receivers[j] = receivers[j], receivers[i]The trade-off: it yields only (n-1)! of the n! permutations, and in groups larger than two it never produces two people who swap gifts with each other. It also ignores your exclusions, so keep the retry loop if you have any.
Send each person only their match
Open each file in the draw folder, copy its one line into a private message to that person, and delete the folder when you're done. Worried someone will lose their message? Keep the files unopened until you need them. Never paste the whole list into a group chat.
Keep the file for next year
Next December, copy last year's pairs into the exclusions table so nobody gets a repeat. The same file can feed your holiday party checklist, since it's already a task list and a table.
Doing this with Carets
This whole setup is one plain-text file plus a script, which is the kind of work Carets is built for. Carets is a fast, native notes and code editor for iPhone, iPad, and Mac, so you can keep the sign-up list as Markdown, edit draw.py with syntax highlighting, and keep both in one project, organized with files, projects, and tags. Your files stay plain text, so they open anywhere, and the same project is there on your phone when someone texts that they're in.
Carets edits the files; you run the script wherever you have Python, such as a Mac terminal.
Get Carets for iPhone, iPad, and Mac on the App Store.
Frequently Asked Questions
Can I run a Secret Santa draw without an app or website?
Yes. A text file with the names and a short script that shuffles them is enough. The script writes each giver's result to its own file, and you send each person only their own line.
What is a derangement and why does it matter for Secret Santa?
A derangement is a permutation in which no element stays in its original position. In a gift exchange that means nobody draws their own name.
Should I use Sattolo's algorithm or retry a shuffle?
Retrying a Fisher–Yates shuffle until nobody matches themselves is simple, and it can produce every valid arrangement, including pairs who swap gifts. Sattolo's variant always produces one big loop, so nobody draws themselves, but it never produces swapped pairs in larger groups.
How do I keep the draw secret if I run it myself?
Don't print the results. Have the script write one file per giver, send each person only theirs, and delete the folder afterwards.