Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • by pudge (1) on 2008.05.15 14:15 (#62872) Homepage Journal
    We recently had our county legislative district caucuses for the Republican Party. The party rules say, in some cases, in the event of a tie, to decide the winner by lot. We had a 7-way tie (with each potential delegate assigned a number), for 6 positions (and since this was for alternate delegates, we needed them in a specific order).

    So I whipped out my laptop and wrote something along the lines of:

    perl -le '@a=qw(6 23 57 72 75 78 80); print $a[rand @a]'
    Got a winner, then removed that winner from @a and kept going until we had the 6 positions filled. Sure, I could have made it a little smarter to remove the winner for me and repeat, but that would have taken a bit more time, and everyone was waiting. :-) Though now that I think about it, I could have used:

    perl -le '@a=qw(6 23 57 72 75 78 80); for (1..6) { print splice, @a, int(rand @a), 1 }'
    • That has a subtle bias: picking 1 out of 6 after picking 1 out of 7 does not have the same probability distribution as picking 2 out of 7. The stochastically unbiased way would be thus:

      perl -MList::Util=shuffle -le'print +(shuffle 6 23 57 72 75 78 80)[0..5]'
      • Err, make that print for (...).

      • That has a subtle bias: picking 1 out of 6 after picking 1 out of 7 does not have the same probability distribution as picking 2 out of 7.

        True, but it doesn't need to. The traditional way to do these things, which is to draw straws, eliminating one person at a time, has the same bias. Indeed, I am not so sure that your way is better: we literally do have a 7-way tie for one place, and then after eliminating the 7th place, we have a 6-way tie for the next place.

        The rules give us no guidance either way, but I think my method is closer to the spirit of the thing (which is why it is how it is). I doubt that anyone would complain with either m

        • I like you're method better simply because the algorithm is more readily open for inspection. Not that you can't check out the contents of the module in the other method, but with yours what happened is up front.

          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
          • If you really wanted to be sure the algorithm is correct, in either case, you’d have to inspect how rand works…

            Writing a Fisher-Yates shuffle (which is what’s in List::Util) by hand is easy: you step through the list, swapping each element with any random following element or itself (this bit is crucial to avoid bias). It was just quicker to use the one in List::Util, and much less prone to errors in the haste of dashing off a one-liner.