>In theory (extrapolating here) you could get caught in a
>infinite loop if the random assignment returned yourself every time
Well, *in theory* yes, but considering that perl can generate well over 100,000 random numbers per second on even a slow machine, the odds of an infinite loop are, well, infinitely small. The odds of it taking over 1 second are pretty slim, too. Nothing to worry about here.
>This script is interesting, I think, as it is typical of
>scripts that must work first time, and you cannot really
>test it over and over with real data as you don't want to
>bug people with junk email. I normally approach this type
>of problem with writing to files and the writing another
>Perl program to test.
Actually, I tested mine extensively - that's how I found the last person "gotcha". A simple rewrite of one line to
takes care of that.open (M, "|more") || die "Could not open more!\n";
>Anyway, just a note to say that my solution, or code
>rather, doesn't have the problem you mention at the end,
>but it was good to point it out. I've never had the
>problem after substantial testing.
Your code *does* have that problem - if everyone else is used up, it assigns a null Kris Kringle. Try this out:
chomp,@_=split(/::/),($e{$_[0]},$l{$_[0]})=($_[1],0)while(<>); for (keys %l) { $l{$_}++; @L=grep{($l{$_}==0)}keys%l; $x=int rand(@L); $l{$L[$x]}++; $l{$_}--; print "$e{$_} has a KK of $L[$x]\n"; }
Now run that with a data set like this:
A::aaron B::bob C::cindy
When aaron and bob get each other, cindy gets nothing:
%type test.txt | perl kk2.pl aaron has a KK of B bob has a KK of A cindy has a KK of
The chance of it happening decreases with the total number of people, but it can still happen....
In reply to RE: RE: RE: Kris Kringle Script
by turnstep
in thread Kris Kringle Script
by Marburg
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |