Pick your language, pick your secret santa.
Several different Implementations of secret santa algorithms, each in a different language.
If you want to write your own secret santa implementation in another language, we're a very welcoming bunch. Send as a pull request and we'll add it!
These implementations solve the secret santa problem in of two ways.
- Randomly shuffle the list of participants
- Each participant receives a gift from the participant that precedes them in the shuffled list
- Each participant buys a gift for the participant that follows them in the shuffled list
- Copy the participants into two lists: buyers and receivers
- Randomly shuffle receivers; do nothing to buyers
- Check the value at every position in buyers and make sure that the corresponding value at the same position in receivers is not the same (the buyer is not buying for him/herself)
- Go back to step (2) if the check in step (3) failed; otherwise we’re done!
Algorithm A isn't able to generate every possible valid combination of assignments, because it cannot produce loops where person A has to buy for person B, and person B has to buy for person A.
Algorithm B is able to generate every valid combination, but will often have to shuffle multiple times to find a correct combination.
This solution contains implementations of both algorithms. select_rotate
is an implementation of Algorithm A, while select_shuffle
is an implementation of Algorithm B.
The only pre-requisite is the erlang compiler. To run in the terminal:
> erl
> c(santa).
> santa:select_rotate(["EmployeeA", "EmployeeB", "EmployeeC", "EmployeeD"]).
Or to run the shuffled solution:
> santa:select_shuffle(["EmployeeA", "EmployeeB", "EmployeeC", "EmployeeD"]).
This function returns an array of tuples matching the secret santa giver to the peson they have to give a gift to. For example:
> santa:select_rotate(["Angus", "Greg", "Mike", "Isabel", "Lewis", "Shannon", "Ally", "Rob"]).
[{"Angus","Shannon"}, {"Shannon","Lewis"}, {"Lewis","Greg"}, {"Greg","Mike"}, {"Mike","Rob"},
{"Rob","Ally"}, {"Ally","Isabel"}, {"Isabel","Angus"}]
The Bash implementation uses a list of email addresses, representing the Secret Santa participants. It implements Algorithm A.
Requires bash 4.0, as the readarray
command is relatively new!
> ./santa.sh
This script will output the list selections to the terminal, but will also attempt to use the mail
command to email each person their recipient. To make it a surprise, simply comment out the line in the actionSanta
function that echo
's to the terminal.
This solution implements a variant on Algorithm B. It works sequentially picking pairs and validating them. It backtracks and picks again if we pick somebody for themselves. If we reach a situation where we are left with only one person picking for themselves we restart.
First install rust (http://www.rust-lang.org/), then run with:
rust run secret-santa.rs
This solution implements Algorithm A.
First install python (http://www.python.org/download/), then run with:
> python secretSanta.py
This solution implements Algorithm B.
First install Java JDK (http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html), then run with:
> javac SecretSanta.java
> java SecretSanta [space-separated list of names]
This implementation uses the following algorithm:
- Loop through participants
- For each participant, get a random giftee from the list of eligible giftees, excluding the participant
- Remove selected giftee from list of eligible giftees
Download clojure and extract files
> java -cp /path/to/clojure.jar clojure.main /path/to/santa.clj
This solution implements Algorithm B.
Run PowerShell console and then run: /path/to/SecretSanta.ps1 In order to run scripts, you may need to first run: Set-ExecutionPolicy Unrestricted PowerShell is included in Windows 7 and above.