in reply to Is this a fair shuffle?

Since the block doesn't reference $a and $b, I'm mystified about what effect it has. Can you explain? (maybe this will be the thing I learn today! I'm not doubting it...just don't understand.)
chas

Replies are listed 'Best First'.
Re^2: Is this a fair shuffle?
by Roy Johnson (Monsignor) on Apr 01, 2005 at 04:27 UTC
    Sort operates by doing pairwise comparisons of elements, applying the comparison function to them. The results of the function tell it how to order the two elements relative to each other. In this case, the results are random, independent of the values being compared, so it sorts into random order.

    Caution: Contents may have been coded under pressure.
      Thanks for both replies! I see now; somehow I was fixated on the fact that the sorting sub or block is supposed to use the values $a, $b, but for a "random" sort, the whole point is that the actual values of $a, $b in each case are supposed to have no (deterministic) effect on the resulting order.
      (By the way, to get a random order, my method would likely be to use rand (in some way) to select one of the 20 numbers, then use it again to select one of the remaining 19, etc. That may be essentially what someone has described in one of the replies already.)
      chas
Re^2: Is this a fair shuffle?
by dmorelli (Scribe) on Apr 01, 2005 at 04:24 UTC
    $a and $b are getting values like 1 and 2 and then 3 and 4, pairs from the list.

    The goal was to shuffle the numbers into a random order, so you don't necessarily want to use the values in the 1..20 range for a comparison.

    Instead, the code is generating a random number and comparing it with .5, for a coin-toss-like chance of one thing being sorted above or below the other thing.