Mix ‘Em Up

Shuffling cards

Shuffling is the process of randomizing a deck of cards. In this project, we will investigate different methods used to shuffle cards, with the goal of comparing how each method randomizes a deck.

This project will require extensive use of the random library.

Exercise: Write a Python function that takes a positive integer as input and returns a “deck” containing the specified number of cards. In other words, you want to return a list of integers between 1 and \(n\).

We will consider three methods of shuffling: the classic riffle, the middle-out method, and the overhand shuffle.

The classic riffle

For this method, the deck is divided roughly in half and the halves held in separate hands. A new deck is built from the bottom up by selecting at random either the bottom card from the left hand or right hand. This is done repeatedly until all the cards are gone from either hand, at which point any remaining cards (in the other hand) are placed on the top of the deck.

The middle-out method

Simpler than the riffle, this method first divides the deck into roughly thirds. The middle third is then placed randomly on the top or the bottom of the deck.

Overhand shuffle

For this shuffle, the originial deck is held in the right hand and the shuffled deck is the left hand. A section of cards is moved from the top of the original deck to the top of the shuffled deck. This is repeated until there are no more cards left in the original deck.

Measuring randomness

Given a list of integers

\[[n_1,n_2,...,n_k],\]

how random are they distributed? One possible measure of randomness is to compute the average distances between adjacent integers

\[\frac{1}{k}\sum_{i=1}^{k-1}|n_{i+1}-n_i|.\]

This measure is fine, but it has some drawbacks. Consider the follwing deck

\[[1,27,2,28,3,29,4,30,...,26,52].\]

The above measure would be about 25.02, about as high as possible; however, we can clearly see that this is not a random deck. How might we modify the measure to avoid this drawback?

Exercise: Write a function that takes a randomized deck as input and returns its measure of randomness.

Project

Part 1: For each of the three shuffling methods, write a function that takes a “deck of cards” as input and returns the shuffled deck.

Part 2: Compare the performance of the methods. Which method acheived the most randomness? Which method achieved an acceptable level of randomness the fastest? Use plots to display your findings.

Extra credit: Write a function to perform a different shuffling method and compare to the other three. For credit, your new shuffle must be physically possible. I may ask you to demonstrate your shuffle for me in office hours.