>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....


In reply to RE: RE: RE: Kris Kringle Script by turnstep
in thread Kris Kringle Script by Marburg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.