# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Rearrange elements of a list

Contributed by:
Wolfram Staff (original content for an out shuffle by Sander Huisman and for a Monge shuffle by Katja Della Libera)

ResourceFunction["Shuffle"][ returns a perfect shuffle of the elments in | |

ResourceFunction["Shuffle"][ repeats the shuffle | |

ResourceFunction["Shuffle"][ performs the shuffle " | |

ResourceFunction["Shuffle"][ repeats the " | |

ResourceFunction["Shuffle"]["Types"] returns a list of implemented shuffle types. |

A shuffle is a permuation of elements in a list.

Shuffle handles lists of arbitrary elements, although the most common application is shuffling a deck of cards.

ResourceFunction["Shuffle"][], ResourceFunction["Shuffle"][*n*], ResourceFunction["Shuffle"]["*type*"] and ResourceFunction["Shuffle"][*n*,"*type*"] represent operator forms of ResourceFunction["Shuffle"] that can be applied to a list.
The specification "*type*" can be one of:

"Out" | perfect out shuffle |

"In" | perfect in shuffle |

"ReverseOut" | reverse out shuffle |

"ReverseIn" | reverse in shuffle |

"DownUnder" | down under shuffle |

"Monge" | Monge shuffle |

"DownMonge" | down Monge shuffle |

"Milk" | milk shuffle |

"RandomSample" | random permutation |

An out shuffle is defined as follows: (1) split the list in two halves; and (2) interweave each half of the list starting with the first half, such that every other element comes from the same half of the list.

Here is an example of a list with 10 items:

When the list has an odd length *n*, the first "half" will be (*n*+1)/2 long and the second "half" will be (*n*-1)/2 long:

The out shuffle keeps the top card on top and the bottom card on bottom of the deck.

An in shuffle is similar to the out shuffle, only the order of the interleaving halves is reversed so the top card in the original deck ends up in the second position from the top.

Perfect shuffles (in and out) are also known as Faro shuffles, weave shuffles or dovetail shuffles.

Reverse perfect shuffles pick every second card in a separate pile and then join the halves, placing the separate pile on top of (for the reverse in shuffle) or under (for the reverse out shuffle) the remaining cards.

In a down under shuffle, one places the top card down on another pile, then places the second card under the bottom of the remaining deck and repeats the procedure until just one card remains.

The down under shuffle is also called the Australian shuffle, in a reference to Australia.

A Monge shuffle is defined as taking the cards from a deck and shuffling them by forming a new deck, alternating between putting the cards from the old deck on the top and bottom of the new deck.

There are two types of Monge shuffles, depending on how the deck is held in the hand—face up or face down— or, equivalently, whether the cards are numbered from top to bottom or from bottom to top.

The down Monge shuffle is equivalent to perforing the Monge shuffle and then reversing the deck.

The Monge shuffle is also known as an over Monge shuffle, Mongean shuffle and Monge's shuffle.

In a milk shuffle, the deck of cards is "milked" by taking a card off the top and bottom and putting them aside, then repeating the milking, adding the milked cards on top of the pile aside.

The milk shuffle is also known as the Klondike shuffle.

ResourceFunction["Shuffle"][*list*] is the same as ResourceFunction["Shuffle"][*list*,"Out"].

ResourceFunction["Shuffle"][…][*list*] is equiavalent to ResourceFunction["Shuffle"][*list*,…].

A perfect shuffle of a list:

In[1]:= |

Out[1]= |

Shuffle several times:

In[2]:= |

Out[2]= |

Use a shuffle operator:

In[3]:= |

Out[3]= |

Shuffle an integer list:

In[4]:= |

Out[4]= |

Or a list of arbitrary expressions:

In[5]:= |

Out[5]= |

Shuffle several times:

In[6]:= |

Out[6]= |

Use a specified shuffle type:

In[7]:= |

Out[7]= |

Repeat the shuffle several times:

In[8]:= |

Out[8]= |

Operator form:

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

A list of implemented shuffles:

In[13]:= |

Out[13]= |

An out shuffle:

In[14]:= |

Out[14]= |

Same as:

In[15]:= |

Out[15]= |

An in shuffle:

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

A reverse out shuffle:

In[18]:= |

Out[18]= |

A reverse in shuffle:

In[19]:= |

Out[19]= |

A down under shuffle:

In[20]:= |

Out[20]= |

A Monge shuffle:

In[21]:= |

Out[21]= |

A down Monge shuffle:

In[22]:= |

Out[22]= |

A milk shuffle:

In[23]:= |

Out[23]= |

Random samples:

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

Use SeedRandom to fix the order of elements:

In[26]:= |

Out[26]= |

In[27]:= |

Out[27]= |

Create a deck of cards:

In[28]:= |

Out[28]= |

Shuffle the deck:

In[29]:= |

Out[29]= |

Use a a series of in and out shuffles to move the top card of a deck down into any desired position; for instance, to place the top card under *n* other cards, express *n* as a binary number and do an in shuffle for each 1 and an out shuffle for each 0:

In[30]:= |

In[31]:= |

In[32]:= |

Out[32]= |

In[33]:= |

Out[33]= |

Permutation cycles of a shuffle:

In[34]:= |

In[35]:= |

Out[35]= |

Visualize as a graph:

In[36]:= |

Out[36]= |

Elements that retain their positions after a shuffle are shown as fixed points on the cycle graph:

In[37]:= |

Out[37]= |

An out shuffle does not change the first element:

In[38]:= |

Out[38]= |

For an even-sized list, an out shuffle also does not change the last element:

In[39]:= |

Out[39]= |

In[40]:= |

Out[40]= |

An in shuffle does not change the last element of an odd-sized list:

In[41]:= |

Out[41]= |

Same as a Monge shuffle:

In[42]:= |

Out[42]= |

A down Monge shuffle preserves the last element of even-sized decks:

In[43]:= |

Out[43]= |

A down under shuffle preserves the first element:

In[44]:= |

Out[44]= |

The order, or period, of a shuffle is the number of shuffles that restores the original sequence of cards in the deck:

In[45]:= |

Out[46]= |

In[47]:= |

Out[47]= |

Shuffle periods for several shuffle types and deck sizes:

In[48]:= |

In[49]:= |

Out[49]= |

The shuffle order of an out shuffle of 2*n* cards is the multiplicative order of 2 modulo 2*n*-1:

In[50]:= |

Out[50]= |

In[51]:= |

Out[51]= |

In[52]:= |

Out[52]= |

Shuffle period for a Monge shuffle of an odd-sized deck is the same as the shuffle period of the even-sized deck with one less card:

In[53]:= |

Out[53]= |

Out and reverse out shuffles are inverses of each other—that is, applying one shuffle after another restores the original order of the deck:

In[54]:= |

Out[54]= |

In[55]:= |

Out[55]= |

And vice versa:

In[56]:= |

Out[56]= |

In[57]:= |

Out[57]= |

Shuffles that are mutual inverses have the same cycle structure and, therefore, the same fixed points and the same shuffle period:

In[58]:= |

Out[58]= |

In[59]:= |

Out[59]= |

If *k* is the shuffle period for a length-*n* list , then the sequence of elements after *m* shuffles is the same as *k*-*m* reverse shuffles:

In[60]:= |

In[61]:= |

Out[61]= |

In[62]:= |

In[63]:= |

Out[63]= |

In[64]:= |

Out[64]= |

In[65]:= |

Out[65]= |

In and reverse in shuffles are mutual inverses:

In[66]:= |

Out[66]= |

In[67]:= |

Out[67]= |

For an even-sized deck, a milk shuffle and a down Monge shuffle are mutual inverses:

In[68]:= |

Out[68]= |

In[69]:= |

Out[69]= |

For an odd-sized deck, a milk shuffle and a Monge shuffle are mutual inverses:

In[70]:= |

Out[70]= |

In[71]:= |

Out[71]= |

Shuffle a deck of cards:

In[72]:= |

Out[72]= |

In[73]:= |

Out[73]= |

In[74]:= |

Out[74]= |

The shuffle periods vs. deck size for an out shuffle:

In[75]:= |

Out[75]= |

Recurring patterns in Monge shuffles of different-length lists:

In[76]:= |

Out[76]= |

Card movement after repeating a Monge shuffle:

In[77]:= |

Out[77]= |

- 1.0.0 – 14 September 2020

This work is licensed under a Creative Commons Attribution 4.0 International License