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

Re: [FWP] Perl Card Games




Hi y'all.

Here is a routine which simulates a "real" shuffle,
in which the deck is cut and the two halves are riffled
together.  It also cuts a small part (about a quarter)
from the top to the bottom of the array first.
This is the sort of thing you would want to call several times
in succession (at least four) before actually using the result.
Even then, it is not a "good" shuffle; it is like a "real" shuffle!

sub real_shuffle(@) {
  @_ > 16 or die "TOO FEW";
  my @a = @_;
  # cut from the top to the bottom, about a quarter of the items:
  push @a, splice @a, 0, (@_>>2) + int(rand 5) - 2;
  # cut into two, about a half of the items:
  my @b = splice @a, 0, (@_>>2) + int(rand 5) - 4;
  my @out;
  while ( @a and @b ) {
    # take a few items from each half.
    # a number in the range ( 1, 1, 2, 3 ).
    push @out, splice @a, 0, ((int(rand 2)+1) * int(rand 2)) + 1;
    push @out, splice @b, 0, ((int(rand 2)+1) * int(rand 2)) + 1;
  }
  # one has been exhausted; grab the remainder of the other.
  @a and push @out, @a;
  @b and push @out, @b;
  @out
}

$, = ' ';
print
  real_shuffle
  real_shuffle
  real_shuffle
  real_shuffle
  real_shuffle
  1..26;

John Porter


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