Secret Santa Name Picker Script
Monday, November 22nd, 2010In the last ten years we’ve been gathering for our annual Pelago holiday party we’ve developed a fun Secret Santa tradition. The theme of the gift exchange is t-shirts. Each person picks a name from a hat to find out the lucky (or unlucky, depending on how you look at it) recipient of a hand-selected t-shirt. With one employee going remote and us being a technology company, it seemed somewhat Ludditean of us to continue picking strips of paper from a box.
This year we automated the process. We wrote a PHP script that would pick the names for us. It starts with an array of email addresses, matches them up in a random order and then emails each person their recipient. And it makes sure no one gets the same person. Take a look at the script below. It’s just a few simple lines of code. Feel free to use it for your next Secret Santa assignments. The basic code is below. Just plug the results in to your favorite email software to secretly distribute the results.
<?php
$emails = array(
'name1@yourcompany.com',
'name2@yourcompany.com',
'name3@yourcompany.com',
'name4@yourcompany.com',
'name5@yourcompany.com',
);
$matches = array();
// randomize a key => value array
$givers = range(0,count($emails)-1);
$givees = range(0,count($emails)-1);
do {
shuffle($givers);
shuffle($givees);
$matches = array_combine($givers, $givees);
foreach ($matches as $er => $ee) {
if ($er == $ee) continue 2;
}
break;
} while (true);
foreach ($matches as $er => $ee) {
echo $emails[$er] . " gives to " . $emails[$ee] . "\n";
}
?>
