Function Repository Resource:

SloppyShuffle

Source Notebook

Return a sloppy cut and shuffle of a list

Contributed by: Ed Pegg Jr

ResourceFunction["SloppyShuffle"][deck]

performs a single shuffle on the list deck.

ResourceFunction["SloppyShuffle"][deck, n]

performs n shuffles on the list deck.

ResourceFunction["SloppyShuffle"][deck, n, slop]

performs n shuffles on the list deck with a sloppiness slop.

Details

SloppyShuffle simulates real-world shuffles by adding randomness in multiple operations.
A sloppy shuffle will have a random start with a random number of cards landing in each part of the interleaving.
Mathematically, these shuffles are known as faro shuffles, after the game faro. More commonly, cutting a deck of cards in half and shuffling them is called a riffle shuffle.
SloppyShuffle randomly chooses between an in shuffle and out shuffle.
An in shuffle is defined as follows: (1) split the list in two halves; and (2) interweave each half of the list starting with the second half, such that every other card came from the same half of the original list.
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 card comes from the same half of the list.

Examples

Basic Examples (2) 

Cut a list of length 20 roughly in half and roughly shuffle the two lists back together:

In[1]:=
ResourceFunction["SloppyShuffle"][Range[20]]
Out[1]=

Shuffle four times:

In[2]:=
ResourceFunction["SloppyShuffle"][Range[10], 4]
Out[2]=

Scope (3) 

For general use, build a deck of cards:

In[3]:=
ranks = Join[CharacterRange["2", "9"], {"10", "J", "Q", "K", "A"}];
suits = {Style["\[ClubSuit]", Black], Style["\[SpadeSuit]", Black], Style["\[HeartSuit]", Red], Style["\[DiamondSuit]", Red]};
deck = Row /@ Tuples[{ranks, suits}]
Out[4]=

Shuffle the deck:

In[5]:=
deck = {
Row[{"2", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"2", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"2", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"2", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"3", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"3", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"3", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"3", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"4", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"4", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"4", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"4", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"5", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"5", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"5", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"5", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"6", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"6", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"6", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"6", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"7", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"7", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"7", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"7", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"8", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"8", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"8", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"8", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"9", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"9", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"9", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"9", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"10", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"10", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"10", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"10", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"J", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"J", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"J", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"J", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"Q", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"Q", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"Q", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"Q", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"K", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"K", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"K", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"K", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"A", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"A", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"A", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"A", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}]};
ResourceFunction["SloppyShuffle"][deck]
Out[6]=

Shuffle the deck seven times:

In[7]:=
ResourceFunction["SloppyShuffle"][deck, 7]
Out[7]=

When the sloppiness of the shuffle increases, the number of possible outcomes increases until it reaches about.3:

In[8]:=
Table[Length[
  Union[Table[
    ResourceFunction["SloppyShuffle"][Range[20], 1, .02 n], {300}]]], {n, 1, 25}]
Out[8]=

Five shuffles usually suffices for a well-mixed deck, but seven shuffles is recommended:

In[9]:=
deck = {
Row[{"2", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"2", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"2", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"2", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"3", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"3", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"3", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"3", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"4", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"4", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"4", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"4", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"5", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"5", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"5", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"5", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"6", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"6", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"6", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"6", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"7", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"7", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"7", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"7", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"8", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"8", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"8", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"8", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"9", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"9", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"9", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"9", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"10", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"10", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"10", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"10", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"J", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"J", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"J", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"J", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"Q", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"Q", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"Q", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"Q", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"K", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"K", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"K", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"K", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"A", 
Style["\[ClubSuit]", 
GrayLevel[0]]}], 
Row[{"A", 
Style["\[SpadeSuit]", 
GrayLevel[0]]}], 
Row[{"A", 
Style["\[HeartSuit]", 
RGBColor[1, 0, 0]]}], 
Row[{"A", 
Style["\[DiamondSuit]", 
RGBColor[1, 0, 0]]}]};
ResourceFunction["SloppyShuffle"][deck, 5]
Out[10]=

Properties and Relations (2) 

Setting zero sloppiness gives a perfect shuffle:

In[11]:=
ResourceFunction["SloppyShuffle"][{a, b, c, d, 1, 2, 3, 4}, 1, 0]
Out[11]=

If the sloppiness is set to zero, there are two possible outcomes after 1 shuffle, based on choosing either an in shuffle or out shuffle:

In[12]:=
Union[Table[ResourceFunction["SloppyShuffle"][Range[20], 1, 0], {20}]]
Out[12]=

Neat Examples (1) 

Use the ResourceFunction "PlayingCardGraphic" to show the top five cards after two shuffles:

In[13]:=
Rasterize@
 ResourceFunction["PlayingCardGraphic"][
  Take[ResourceFunction["SloppyShuffle"][Range[52], 2], 5], "CardOffset" -> {0, 0}]
Out[13]=

Version History

  • 1.0.0 – 02 March 2026

Related Resources

License Information