in reply to Perfectly shuffling a deck of cards, over and over
japhy has a one-liner to do either-sided Riffle shuffles at Array One-liners. You can make this really succinct if you also use glob to generate the initial deck:
It might also be interesting to see what happens when you change the riffle sub to:sub riffle { splice @_, @_/2 - $_, 0, pop for 0 .. (@_/2)-1; return @_; } my @deck = glob "{2,3,4,5,6,7,8,9,T,J,K,Q,A}{C,D,H,S}"; for my $shuffle (1 .. 10) { @deck = riffle @deck; print "shuffle #$shuffle:\n@deck\n"; }
## random-sided Riffle shuffle sub riffle { if (rand() < 0.5) { splice @_, @_/2 - $_, 0, pop for 0 .. (@_/2)-1; } else { splice @_, @_/2 + $_, 0, shift for 0 .. (@_/2)-1; } return @_; }
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perfectly shuffling a deck of cards, over and over
by Anonymous Monk on Jul 10, 2004 at 03:03 UTC | |
by Enlil (Parson) on Jul 10, 2004 at 05:31 UTC | |
by Mr. Muskrat (Canon) on Jul 10, 2004 at 04:45 UTC |