Every December someone ships a Secret Santa app, and every December the same three things go wrong: the shuffle loops forever on a tricky exclusion list, the "private" assignment link contains the recipient's email in plain text, and the organizer dashboard quietly shows the whole pairing in a mode that was supposed to be blind.
I built one this year. These are the parts that were not obvious, with the code that ended up working.
1. A shuffle is not a draw
The obvious implementation is: shuffle the participant list, pair everyone with the next person, check the rules, retry if it fails.
// don't do this do { names = shuffle ( names ) } while ( ! isValid ( names ) )
Enter fullscreen mode
Exit fullscreen mode
This has two problems. With a tight exclusion list (couples, families) the valid-permutation rate collapses and the loop runs for a long time.…