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

Re: [FWP] Perl Card Games



On Tue, Aug 03, 1999 at 05:18:18PM -0400, Chip Turner wrote:
> 
> @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);

Untested, I presume :-)  Yours clobbers cards instead of swapping
them.

This is similar, but actually works:

  @cards = (0 .. 51); # or any number of them
  $passes = 1; # one pass should be sufficient for all but the most paranoid
  foreach (0 .. ($passes * @cards - 1)) {
    $rand = rand(@cards);
    @cards[$rand % @cards, $_ % @cards] = @cards[$_ % @cards, $rand % @cards];
  }

Unfortunately, it lacks the nifty foreach-as-a-statement-modifier.

'Course, if it was me, I'd just do

  use Algorithms::Numerical::Shuffle;
  @cards = (0 .. 51); # or any number of them
  @cards = shuffle(@cards)

Not as much fun, maybe, but a lot quicker to type and a lot less
error-prone :-)


        Brand

P.S., The Shuffle module is available here:

      http://www.perl.com/CPAN/authors/id/ABIGAIL/

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