'Drawing Names' for Christmas
Back in the days of the Internet Bubble, when stocks of which I owned none were doing so very well, it was expected that everyone in the family would buy at least one gift for everyone else in the family. n * (n - 1) gifts. Ridiculous.
When the word budget was once again a part of the vernacular, I was called upon to write a program to "draw names". The mark of poverty. This was December of 2000. Behold, my creation:
#!/usr/bin/perl -w
use strict;
# this is how we generate the random seed value.
srand( time ^ $$ ^ unpack "%32L*", `ps wwaxl | gzip` );
# Obviously, the list of names.
my $list = [
{name=>'Curtis', match=>-1},
{name=>'Kevin', match=>-1},
{name=>'Natalie', match=>-1},
{name=>'Aimee', match=>-1}
];
# Each time a person's name is drawn, we put it in here,
# so that they are not drawn again.
my $done = [];
# for each person in the list, find a recipient for them.
for my $x (0..$#$list) {
CJ: while ( 1 ) {
my $r = int(rand(@{$list}));
if( $done->[$r] || $r == $x ) {
next CJ;
}
else {
$list->[$x]{match} = $r;
$done->[$r] = 1;
last CJ;
}
}
}
for my $x (0..$#$list) {
print $list->[$x]{name} . " drew " .
$list->[$list->[$x]{match}]{name} . "\n";
}
Just dump this amazing program into a file with a .pl extension (ie, Perl), and run it ... you do have Perl installed on your machine, right?? Windows, you say? I didn't know people still used that. Does it still let you over-write the contents of main memory by doing this:
int
main (int argc, char **argv)
{
int my_array[10];
int * ptr = (int *)&my_array;
while (1)
*(ptr++) = 0xfeedface;
return;
}
That poor machine in CS class....
Update: Some bastard is trying to rip off MY idea. May a thousand bugs descend upon his code. :-)