Card shuffles

From Rosetta Code
Card shuffles is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

There are many techniques that people use to shuffle cards for card games. Some are more effective than others.

The task here is to implement the (seemingly) more common techniques of the riffle shuffle and overhand shuffle for n iterations. Implementing playing cards is not necessary if it would be easier to implement these shuffling methods for generic collections. Where possible, compare this to a standard/built-in shuffling procedure.

One iteration of the riffle shuffle is defined as:

  1. Split the deck into two piles
  2. Merge the two piles by alternating taking one card from the top or bottom (the same throughout the whole merge) of each pile
  3. The merged deck is now the new "shuffled" deck

One iteration of the overhand shuffle is defined as:

  1. Take a group of consecutive cards from the top of the deck. For our purposes up to 20% of the deck seems like a good amount.
  2. Place that group on top of a second pile
  3. Repeat these steps until there are no cards remaining in the original deck
  4. The second pile is now the new "shuffled" deck

Bonus: Implement other methods described here. Allow for "human errors" of imperfect cutting and interleaving.

C++[edit]

 
#include <time.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <deque>
 
 
class riffle
{
public:
void shuffle( std::deque<int>* v, int tm )
{
std::deque<int> tmp;
bool fl;
size_t len;
std::deque<int>::iterator it;
 
copyTo( v, &tmp );
 
for( int t = 0; t < tm; t++ )
{
std::deque<int> lHand( rand() % ( tmp.size() / 3 ) + ( tmp.size() >> 1 ) ), rHand( tmp.size() - lHand.size() );
 
std::copy( tmp.begin(), tmp.begin() + lHand.size(), lHand.begin() );
std::copy( tmp.begin() + lHand.size(), tmp.end(), rHand.begin() );
tmp.clear();
 
while( lHand.size() && rHand.size() )
{
fl = rand() % 10 < 5;
if( fl )
len = 1 + lHand.size() > 3 ? rand() % 3 + 1 : rand() % ( lHand.size() ) + 1;
else
len = 1 + rHand.size() > 3 ? rand() % 3 + 1 : rand() % ( rHand.size() ) + 1;
 
while( len )
{
if( fl )
{
tmp.push_front( *lHand.begin() );
lHand.erase( lHand.begin() );
}
else
{
tmp.push_front( *rHand.begin() );
rHand.erase( rHand.begin() );
}
len--;
}
}
 
if( lHand.size() < 1 )
{
for( std::deque<int>::iterator x = rHand.begin(); x != rHand.end(); x++ )
tmp.push_front( *x );
}
if( rHand.size() < 1 )
{
for( std::deque<int>::iterator x = lHand.begin(); x != lHand.end(); x++ )
tmp.push_front( *x );
}
}
copyTo( &tmp, v );
}
private:
void copyTo( std::deque<int>* a, std::deque<int>* b )
{
for( std::deque<int>::iterator x = a->begin(); x != a->end(); x++ )
b->push_back( *x );
a->clear();
}
};
 
class overhand
{
public:
void shuffle( std::deque<int>* v, int tm )
{
std::deque<int> tmp;
bool top;
for( int t = 0; t < tm; t++ )
{
while( v->size() )
{
size_t len = rand() % ( v->size() ) + 1;
top = rand() % 10 < 5;
while( len )
{
if( top ) tmp.push_back( *v->begin() );
else tmp.push_front( *v->begin() );
v->erase( v->begin() );
len--;
}
}
for( std::deque<int>::iterator x = tmp.begin(); x != tmp.end(); x++ )
v->push_back( *x );
 
tmp.clear();
}
}
};
 
// global - just to make things simpler ---------------------------------------------------
std::deque<int> cards;
 
void fill()
{
cards.clear();
for( int x = 0; x < 20; x++ )
cards.push_back( x + 1 );
}
 
void display( std::string t )
{
std::cout << t << "\n";
for( std::deque<int>::iterator x = cards.begin(); x != cards.end(); x++ )
std::cout << *x << " ";
std::cout << "\n\n";
}
 
int main( int argc, char* argv[] )
{
srand( static_cast<unsigned>( time( NULL ) ) );
riffle r; overhand o;
 
fill(); r.shuffle( &cards, 10 ); display( "RIFFLE" );
fill(); o.shuffle( &cards, 10 ); display( "OVERHAND" );
fill(); std::random_shuffle( cards.begin(), cards.end() ); display( "STD SHUFFLE" );
 
return 0;
}
 
Output:
RIFFLE
18 9 17 20 3 4 16 8 7 10 5 14 12 1 13 19 2 11 15 6

OVERHAND
2 13 12 11 10 9 18 17 6 5 4 3 7 20 19 15 8 14 16 1

STD SHUFFLE
14 4 17 3 12 5 19 6 20 2 16 11 8 15 7 13 10 18 9 1

J[edit]

Generally, this task should be accomplished in J using ({~ ?~@#). Here we take an approach that's more comparable with the other examples on this page.
NB. overhand cut
overhand=: (\: [: +/\ %@%:@# > # ?@# 0:)@]^:[
 
NB. Gilbert–Shannon–Reeds model
riffle=: (({.~+/)`(I.@])`(-.@]#inv (}.~+/))} ?@(#&2)@#)@]^:[

Overhand shuffle is implemented not as was described in wikipedia but as described on the talk page "the cuts are taken from the top of the deck and placed on top of the new deck". The probability of a cut occurring between each pair of cards in this overhand shuffle is proportional to the reciprocal of the square root of the number of cards in the deck.

In other words, overhand cut breaks the deck into some number of pieces and reverses the order of those pieces.

Here are some examples of the underlying selection mechanism in action for a deck of 10 cards:

   ([: +/\ %@%:@# > # ?@# 0:) i.10
0 0 0 0 0 0 0 0 1 1
([: +/\ %@%:@# > # ?@# 0:) i.10
1 1 2 2 2 3 3 3 3 3
([: +/\ %@%:@# > # ?@# 0:) i.10
0 1 1 2 3 3 3 3 4 5
([: +/\ %@%:@# > # ?@# 0:) i.10
0 1 1 1 1 2 2 3 3 3

The final step of a cut is to sort the deck in descending order based on the numbers we compute this way.

The left argument says how many of these cuts to perform.

Task examples:

   1 riffle i.20
0 1 2 3 4 5 6 7 8 13 14 9 15 16 17 10 18 11 12 19
10 riffle i.20
6 10 13 8 2 14 15 9 19 3 18 16 11 1 12 17 5 4 0 7
1 overhand i.20
17 18 19 13 14 15 16 4 5 6 7 8 9 10 11 12 0 1 2 3
10 overhand i.20
15 11 2 4 5 12 16 10 17 19 9 8 6 13 3 18 7 1 0 14

Java[edit]

Works with: Java version 1.5+
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
 
public class CardShuffles{
 
private static final Random rand = new Random();
 
public static <T> LinkedList<T> riffleShuffle(List<T> list, int flips){
LinkedList<T> newList = new LinkedList<T>();
 
newList.addAll(list);
 
for(int n = 0; n < flips; n++){
//cut the deck at the middle +/- 10%, remove the second line of the formula for perfect cutting
int cutPoint = newList.size() / 2
+ (rand.nextBoolean() ? -1 : 1 ) * rand.nextInt((int)(newList.size() * 0.1));
 
//split the deck
List<T> left = new LinkedList<T>();
left.addAll(newList.subList(0, cutPoint));
List<T> right = new LinkedList<T>();
right.addAll(newList.subList(cutPoint, newList.size()));
 
newList.clear();
 
while(left.size() > 0 && right.size() > 0){
//allow for imperfect riffling so that more than one card can come form the same side in a row
//biased towards the side with more cards
//remove the if and else and brackets for perfect riffling
if(rand.nextDouble() >= ((double)left.size() / right.size()) / 2){
newList.add(right.remove(0));
}else{
newList.add(left.remove(0));
}
}
 
//if either hand is out of cards then flip all of the other hand to the shuffled deck
if(left.size() > 0) newList.addAll(left);
if(right.size() > 0) newList.addAll(right);
}
return newList;
}
 
public static <T> LinkedList<T> overhandShuffle(List<T> list, int passes){
LinkedList<T> mainHand = new LinkedList<T>();
 
mainHand.addAll(list);
for(int n = 0; n < passes; n++){
LinkedList<T> otherHand = new LinkedList<T>();
 
while(mainHand.size() > 0){
//cut at up to 20% of the way through the deck
int cutSize = rand.nextInt((int)(list.size() * 0.2)) + 1;
 
LinkedList<T> temp = new LinkedList<T>();
 
//grab the next cut up to the end of the cards left in the main hand
for(int i = 0; i < cutSize && mainHand.size() > 0; i++){
temp.add(mainHand.remove());
}
 
//add them to the cards in the other hand, sometimes to the front sometimes to the back
if(rand.nextDouble() >= 0.1){
//front most of the time
otherHand.addAll(0, temp);
}else{
//end sometimes
otherHand.addAll(temp);
}
}
 
//move the cards back to the main hand
mainHand = otherHand;
}
return mainHand;
}
 
public static void main(String[] args){
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
System.out.println(list);
list = riffleShuffle(list, 10);
System.out.println(list + "\n");
 
list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
System.out.println(list);
list = riffleShuffle(list, 1);
System.out.println(list + "\n");
 
list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
System.out.println(list);
list = overhandShuffle(list, 10);
System.out.println(list + "\n");
 
list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
System.out.println(list);
list = overhandShuffle(list, 1);
System.out.println(list + "\n");
 
list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list + "\n");
}
}
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 11, 1, 9, 15, 4, 19, 16, 8, 13, 7, 2, 14, 12, 10, 3, 17, 18, 6, 5]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 12, 2, 3, 4, 5, 13, 14, 15, 6, 16, 7, 8, 9, 17, 18, 10, 19, 20, 11]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 3, 10, 4, 2, 8, 1, 18, 13, 19, 14, 6, 9, 12, 16, 15, 5, 7, 11, 17]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[18, 19, 20, 17, 13, 14, 15, 16, 9, 10, 11, 12, 8, 6, 7, 3, 4, 5, 1, 2]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[18, 12, 13, 14, 2, 3, 15, 5, 9, 19, 7, 11, 1, 6, 4, 20, 16, 17, 10, 8]

PARI/GP[edit]

Riffle shuffle:

riffle(v)=
{
my(n=#v,k,t,deck=vector(n),left,right);
t=random(2^n);
for(i=0,n,
t -= binomial(n,i);
if(t<0, k=i; break)
);
if(k==0||k==n, return(v));
left=k;
right=n-k;
deck=vector(n,i,
t=random(n+1-i);
v[if(t<left, k-left--, n-right--)]
);
vecextract(v, deck);
}
addhelp(riffle, "riffle(v): Riffle shuffles the vector v, following the Gilbert-Shannon-Reeds model.");

Overhand shuffle:

overhand(v)=
{
my(u=[],t,n=2*#v\5);
while(#v,
t=min(random(n)+1,#v);
u=concat(v[1..t],u);
v=if(t<#v,v[t+1..#v],[]);
);
u;
}
addhelp(overhand, "overhand(v): Overhand shuffles the vector v.");

Usage:

riffle([1..52])
overhand([1..52])
Output:
%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52]
%2 = [44, 45, 46, 47, 48, 49, 50, 51, 52, 43, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4]

Perl 6[edit]

use v6;
 
sub overhand ( @cards ) {
my @splits = roll 10, ^( @cards.elems div 5 )+1;
@cards.rotor( @splits ,:partial ).reverse.flat
}
 
sub riffle ( @pile is copy ) {
my @pile2 = @pile.splice: @pile.elems div 2 ;
 
roundrobin(
@pile.rotor( (1 .. 3).roll(7), :partial ),
@pile2.rotor( (1 .. 3).roll(9), :partial ),
).flat
}
 
my @cards = ^20;
@cards.=&overhand for ^10;
say @cards;
 
my @cards2 = ^20;
@cards2.=&riffle for ^10;
say @cards2;
 
say (^20).pick(*);
 

Racket[edit]

These implementations are in typed/racket, which means that additional annotations are needed which looks like hard work.

On the bright side, if you want to add a new Cutter or Riffler, DrRacket will let you know immediately if you're consuming lists of lists of lists at the right depth and in the right quantities.

Racket has a built in shuffle function. Frankly, I'd go with that in your own code!

#lang typed/racket
;; ---------------------------------------------------------------------------------------------------
;; Types and shuffle builder
 
;; A cutter separates the deck into more than one sub-decks -- the last one of these is "left in the
;; hand", as per the overhand shuffle (since it is the last strip to be stripped). The riffler
;; indicates this in its second (non-null) return value
(define-type (Cutter A) (-> (Listof A) (Pair (Listof A) (Listof (Listof A)))))
;; A riffler takes taking hand and the cut deck parts. returns a newly merged deck in the "taking"
;; hand and the deck left in the "giving" hand. The shuffler will keep taking,
;; until there is nothing to give
(define-type (Riffler A) ((Listof A) (Listof A) (Listof A) * -> (Values (Listof A) (Listof A))))
;; "The shuffler will keep taking until there is nothing to give"... and will do this
;; the number of times specified by its second argument
(define-type (Shuffler A) ((Listof A) Natural -> (Listof A)))
 
;; makes a shuffler from the cutter and the riffler
(: shuffler-composer (All (A) (Cutter A) (Riffler A) -> (Shuffler A)))
(define ((shuffler-composer cut riffle) deck n)
(: one-shuffle : (Listof A) -> (Listof A))
(define (one-shuffle g)
(let: shuff ((t : (Listof A) null) (g : (Listof A) g))
(let-values (((t+ g-) (apply riffle t (cut g))))
(if (null? g-) t+ (shuff t+ g-)))))
(for/fold : (Listof A) ((d deck)) ((i (in-range n)))
(one-shuffle d)))
 
;; convenient wrapper around the above (otherwise we'd need the inst every time we
;; wanted to compose a cut and a riffle
(define-syntax-rule (define-composed-shuffler s (c r))
(define: (A) (s [x : (Listof A)] [n : Natural]) : (Listof A)
((#{shuffler-composer @ A} c r) x n)))
 
;; ---------------------------------------------------------------------------------------------------
;; Overhand (and, as far as I can tell, Indian)
(: overhand-cutter (All (A) (Cutter A)))
(: overhand-riffler (All (A) (Riffler A)))
 
(define (overhand-cutter l)
(define spl (match (length l) [0 0] [1 1] [len (add1 (random (sub1 len)))]))
(list (take l spl) (drop l spl)))
 
(define (overhand-riffler t p1 . rest)
(values (append p1 t) (append* rest)))
 
(define-composed-shuffler overhand-shuffle (overhand-cutter overhand-riffler))
 
;; ---------------------------------------------------------------------------------------------------
;; Riffle (with optional "drop" where two cards are riffled
(: half-deck-cutter (All (A) (Cutter A)))
(: mk-riffle-riffler (All (A) ((#:p-drop Nonnegative-Real) -> (Riffler A))))
 
(define (half-deck-cutter l)
(define spl (quotient (length l) 2))
(list (take l spl) (drop l spl)))
 
;; All the "reverse"ing is to emulate a physical shuffle... it's not
;; necessary for the "randomising" effect (which there isn't really on
;; a pure riffle anyway)
;;
;; Additional complexity added by ability to drop cards on both taking
;; and giving hand
(define ((mk-riffle-riffler #:p-drop (p-drop 0)) t p1 . rest)
(define g-/rev
(let R : (Listof A)
((r1 : (Listof A) p1)
(r2 : (Listof A) (append* rest))
(rv : (Listof A) t)) ; although t should normaly be null
(define drop-t? (< (random) p-drop))
(define drop-g? (< (random) p-drop))
(match* (r1 r2 drop-t? drop-g?)
[((list) (app reverse 2r) _ _) (append 2r rv)]
[((app reverse 1r) (list) _ _) (append 1r rv)]
[((list a1.1 a1.2 d1 ...) (list a2.1 a2.2 d2 ...) #t #t)
(R d1 d2 (list* a2.2 a2.1 a1.2 a1.1 rv))]
[((list a1.1 a1.2 d1 ...) (list a2.1 d2 ...) #t _)
(R d1 d2 (list* a2.1 a1.2 a1.1 rv))]
[((list a1.1 d1 ...) (list a2.1 a2.2 d2 ...) _ #t)
(R d1 d2 (list* a2.2 a2.1 a1.1 rv))]
[((list a1.1 d1 ...) (list a2.1 d2 ...) _ _)
(R d1 d2 (list* a2.1 a1.1 rv))])))
(values (reverse g-/rev) null))
 
(define-composed-shuffler pure-riffle-shuffle (half-deck-cutter (mk-riffle-riffler)))
(define-composed-shuffler klutz-riffle-shuffle (half-deck-cutter (mk-riffle-riffler #:p-drop 0.5)))
 
;; ---------------------------------------------------------------------------------------------------
;; Pile Shuffle
;; Also Wash Shuffle, if pile-height=1 and random-gather=#t
(: mk-pile-cutter (All (A) (#:pile-height Positive-Integer -> (Cutter A))))
(: mk-pile-riffler (All (A) ((#:random-gather? Boolean) -> (Riffler A))))
 
(define ((mk-pile-cutter #:pile-height pile-height) l)
(define len-l (length l))
(define n-piles (add1 (quotient (sub1 len-l) pile-height)))
(: make-pile (Integer -> (Listof A)))
(define (make-pile n)
(for/list : (Listof A) ((i (in-range n len-l n-piles)))
(list-ref l i)))
(define pile-0 (make-pile 0))
(define piles-ns (for/list : (Listof (Listof A)) ((n (in-range 1 n-piles))) (make-pile n)))
(list* pile-0 piles-ns))
 
(define ((mk-pile-riffler #:random-gather? (random-gather? #f)) t p1 . rest)
(: piles (Listof (Listof A)))
(define piles (cons p1 rest))
(define gather (if random-gather? (shuffle piles) piles))
(values (append* (cons t (if random-gather? (shuffle piles) piles))) null))
 
(define-composed-shuffler 4-high-pile-shuffle ((mk-pile-cutter #:pile-height 4) (mk-pile-riffler)))
(define-composed-shuffler wash-pile-shuffle
((mk-pile-cutter #:pile-height 1) (mk-pile-riffler #:random-gather? #t)))
 
;; ---------------------------------------------------------------------------------------------------
(define unshuffled-pack
(for*/list : (Listof String)
((s '(♥ ♦ ♣ ♠))
(f '(2 3 4 5 6 7 8 9 T J Q K A)))
(format "~a~a" f s)))
 
;; ---------------------------------------------------------------------------------------------------
;; TEST/OUTPUT
(module+ test
(require typed/rackunit)
(check-equal? (overhand-shuffle null 1) null)
(check-equal? (overhand-shuffle '(a) 1) '(a))
(check-equal? (overhand-shuffle '(a b) 1) '(b a))
(check-equal? (pure-riffle-shuffle '(1 2 3 4) 1) '(1 3 2 4))
(error-print-width 80))
 
(module+ main
(printf "deck (original order): ~.a~%" unshuffled-pack)
(printf "overhand-shuffle (2 passes): ~.a~%" (overhand-shuffle unshuffled-pack 2))
(printf "overhand-shuffle (1300 passes): ~.a~%" (overhand-shuffle unshuffled-pack 1300))
(printf "riffle: pure ~.a~%" (pure-riffle-shuffle unshuffled-pack 1))
(printf "riffle: klutz ~.a~%" (klutz-riffle-shuffle unshuffled-pack 1))
(printf "4-high piles: ~.a~%" (4-high-pile-shuffle unshuffled-pack 1))
(printf "4-high piles (7 passes): ~.a~%" (4-high-pile-shuffle unshuffled-pack 7))
(printf "4-high piles (7 passes again): ~.a~%" (4-high-pile-shuffle unshuffled-pack 7))
(printf "wash piles: ~.a~%" (wash-pile-shuffle unshuffled-pack 1))
 ;; Or there is always the built-in shuffle:
(printf "shuffle: ~.a~%" (shuffle unshuffled-pack)))
Output:

You see no output from the tests... that's a good thing, they're all passing.

Output is truncated by the ~.a format in printf. However, this should give you some idea of what's going on.

deck (original order):          (2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ T♥ J♥ Q♥ K♥ A♥ 2♦ 3♦ 4...
overhand-shuffle (2 passes):    (2♥ 6♠ 5♠ J♦ Q♦ K♦ A♦ 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ T...
overhand-shuffle (1300 passes): (J♦ J♥ J♠ A♥ K♦ 5♥ J♣ 8♣ 2♥ 4♠ 9♥ A♠ K♣ Q♥ 4♥ 7...
riffle: pure                    (2♥ 2♣ 3♥ 3♣ 4♥ 4♣ 5♥ 5♣ 6♥ 6♣ 7♥ 7♣ 8♥ 8♣ 9♥ 9...
riffle: klutz                   (2♥ 2♣ 3♥ 3♣ 4♥ 4♣ 5♣ 5♥ 6♥ 6♣ 7♥ 7♣ 8♥ 8♣ 9♥ 9...
4-high piles:                   (2♥ 2♦ 2♣ 2♠ 3♥ 3♦ 3♣ 3♠ 4♥ 4♦ 4♣ 4♠ 5♥ 5♦ 5♣ 5...
4-high piles (7 passes):        (2♥ 6♥ T♥ A♥ 5♦ 9♦ K♦ 4♣ 8♣ Q♣ 3♠ 7♠ J♠ 3♥ 7♥ J...
4-high piles (7 passes again):  (2♥ 6♥ T♥ A♥ 5♦ 9♦ K♦ 4♣ 8♣ Q♣ 3♠ 7♠ J♠ 3♥ 7♥ J...
wash piles:                     (4♣ K♠ 4♠ Q♥ J♣ A♣ 6♦ 6♥ 7♥ A♠ T♠ T♥ Q♣ 8♠ 3♣ J...
shuffle:                        (J♣ 2♠ 4♦ A♦ K♥ 6♦ 5♦ 8♣ 2♦ T♥ 4♠ 3♣ 7♦ 9♠ T♦ J...

Tcl[edit]

 
proc riffle deck {
set length [llength $deck]
for {set i 0} {$i < $length/2} { incr i} {
lappend temp [lindex $deck $i] [lindex $deck [expr {$length/2+$i}]]}
set temp}
proc overhand deck {
set cut [expr {[llength $deck] /5}]
for {set i $cut} {$i >-1} {incr i -1} {
lappend temp [lrange $deck [expr {$i *$cut}] [expr {($i+1) *$cut -1}] ]}
concat {*}$temp}
puts [riffle [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]]
puts [overhand [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]]
 

zkl[edit]

A much better shuffle is List's shuffle method.

fcn riffle(deck){
len,N:=deck.len(),len/2;
newDeck:=N.pump(List,'wrap(n){ return(Void.Write,deck[n],deck[N+n]) });
if(len.isOdd) return(newDeck.append(deck[-1]));
newDeck
}
fcn overHand(deck){
len,N,piles:=deck.len(),(0.2*len).toInt(),(len.toFloat()/N).ceil().toInt();
piles.pump(List,'wrap(n){ deck[n*N,N] }).reverse().flatten()
}
riffle(  [1..19].walk()).println();
overHand([1..19].walk()).println();
[1..19].walk().shuffle().println();
Output:
L(1,10,2,11,3,12,4,13,5,14,6,15,7,16,8,17,9,18,19)
L(19,16,17,18,13,14,15,10,11,12,7,8,9,4,5,6,1,2,3)
L(9,11,12,6,17,18,5,10,8,19,2,15,4,3,13,1,7,14,16)