>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
open (M, "|more") || die "Could not open more!\n";
takes care of that.
>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....
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.