[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Perl Card Games



1999-08-03-21:18:18 Chip Turner:
> @cards = (0 .. 51); # or any number of them
> $passes = 1; # one pass should be sufficient for all but the most paranoid
> $cards[$_ % @cards] = $cards[rand(@cards) % @cards] foreach 0 .. ($passes * @cards - 1);

Cool! But not quite correct, I don't think; in your assignment you are
generally overwriting one card with another, so in just one pass when I just
tested that squashed down to 20 distict cards, with many duplicates. You need
a swap if you want to do it in-place. And rather than leaving the domain of
the swap open, I'd rather have it be "from the remainder". Hence my preferred
shuffling copy, or the swap version:

  @cards = (0 .. 30);
  for my $i (0 .. $#cards-1) {
  	@cards[$i, $_] = @cards[$_, $i] for $i + rand(@cards-$i);
  }

-Bennett

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe