The (Computer) Science Behind Your Music App’s Shuffle Algorithm

By: Patricia Kusumaningtyas |

Let’s say you are a software engineer in a music streaming company. Your boss just asked you to create a shuffle algorithm for the shuffle button; the shuffle should be as smooth as possible, leaving no songs with the same artist/genre played next to each other. You have a task to satisfy the company’s customers by making the shuffle algorithm as even as possible. Simple, you might think, initially, I’ll just use a purely random generator. By purely random generator, you mean it’s like pulling options out of a box—totally unprecedented, clean, and by chance.

So, you create your algorithm based on the flowchart above. You give it to your boss, they distribute it, et cetera, but soon after, you get a lot of complaints about how the shuffle algorithm is not that even. Customers get as much as five songs from the same genre/artist in a row. You think, How could this be? I created this shuffle generator as randomly as possible. There is no way this could happen.

Let’s think about this is a mathematical point of view. When you pull five pieces of paper from a box, without considering other factors (position of your hand, depth of the box, etc), the five pieces of paper is equally likely to be picked. The same thing goes with a music application. Let’s say you have a playlist with six songs: three Ariana Grande songs, two Lady Gaga songs, and one Taylor Swift song. With six different songs, you have 720 different combinations, in which 720 of them are equally likely to appear. That means this uneven combination:

is as equally likely to appear as this perfectly random combination:

And then everything comes back to one’s definition of unequal. For example, if one’s definition of unequal is three Ariana Grande songs in a row—which is the barest minimum definition of “unequal”—then there would be 144 “unequal” shuffles out of 720, which means ⅕ of the time, your shuffle would be unequal. We didn’t even count the Lady Gaga songs, or the possibility of the three Ariana Grande being played with only one different song between them. This is why the purely random shuffle does not work. In fact, most music streaming companies’ shuffle algorithm is not just purely shuffling.

So, we have to create a different algorithm—a smarter one. This smarter one has to hold a memory of a previous song and tests it using two conditions: checking the artist and the genre. You create two versions of this algorithm. The first one is:

This version works better than the previous one in terms of the even-ness, but when you test it, you have a slower app. This is because the algorithm goes through all the songs all over again without thinking about what song that was previously discarded.

Your second solution goes like this:

This version also works, but now your music app has to utilize a bigger memory space since you have to remember all the songs you let go when the algorithm chooses the next shuffle song.

Well, there is a compromise in each option, you thought. You let your boss choose. But at least, now you know, a simple random shuffle won’t cut it in making your music app shuffle songs evenly.

Further reading:

  1. How to Shuffle Songs?—Spotify Labs:
  2. Why ‘Random’ Shuffle Feels Far from Random—The Independent:
  3. Is iTunes’s Shuffle Mode Truly Random?—Lifewire:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s