Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Rearrange elements of a list
ResourceFunction["Shuffle"][list] returns a perfect shuffle of the elments in list. |
|
ResourceFunction["Shuffle"][list,n] repeats the shuffle n times. |
|
ResourceFunction["Shuffle"][list,"type"] performs the shuffle "type". |
|
ResourceFunction["Shuffle"][list,n,"type"] repeats the "type" shuffle n times. |
|
ResourceFunction["Shuffle"]["Types"] returns a list of implemented shuffle types. |
"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 |
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 2n cards is the multiplicative order of 2 modulo 2n-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]= |
![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License