Live model · Spotify shuffle
How Spotify Shuffle Actually Works
A truly random shuffle plays the same artist back to back more often than you'd guess, because clumped orders are exactly as likely as spread-out ones. Listeners heard that as broken. In 2014 Spotify described its fix: spread each artist's songs across the playlist, from a random start, with a little jitter. Both run below.
- D1
- A3
- B4
- A1
- B3
- B1
- A4
- A6
- D2
- C1
- C2
- A2
- D3
- B2
- A5
- C3
- CH1 Artist A
- CH2 Artist B
- CH3 Artist C
- CH4 Artist D
fisherYates() on 16 songs, seed 0x3F2A. Done: the same artist plays back to back 3 times. Outlined pairs are the repeats.
Table
| Same artist back to back, per shuffle | Pure random | Spread out |
|---|---|---|
| 0 | 1.5% (30) | 57.8% (1155) |
| 1 | 9.1% (182) | 38.0% (759) |
| 2 | 18.9% (378) | 4.3% (86) |
| 3 | 26.4% (528) | 0.0% (0) |
| 4 | 22.2% (444) | 0.0% (0) |
| 5 | 13.1% (262) | 0.0% (0) |
| 6 | 5.8% (116) | 0.0% (0) |
| 7 | 2.4% (47) | 0.0% (0) |
| 8+ | 0.7% (13) | 0.0% (0) |
Why a fair shuffle clumps
A fair shuffle has no memory of what it just played, so nothing keeps an artist's songs apart. On this 16-song playlist, with 6 songs by artist A, a fair shuffle averages about 3.4 pairs of neighbours by the same artist. The histogram counts 2,000 shuffles of each kind, so you see every outcome, not one lucky run. Slide artist A up and the clumps grow.
How the spread-out shuffle works
Step through it. Each artist's songs get spaced evenly across the playlist: an artist with 4 songs gets one every quarter of the way. A random offset sets where the first lands, and each song is nudged a little so the gaps aren't exact. Then everything plays in order of where it landed.
The spread-out shuffle, one artist at a time
- CH1 Artist A
- CH2 Artist B
- CH3 Artist C
- CH4 Artist D
Run it on your playlist
Open a playlist you shuffle and count the songs by its top artists, up to four of them and 12 songs each. Type the counts below. The model shuffles your playlist 2,000 times each way. If pure random's average lands on what the math predicts for a fair shuffle, the clumps you hear are what fair sounds like.
Table
| Same artist back to back, per shuffle | Pure random | Spread out |
|---|---|---|
| 0 | 1.5% (30) | 57.8% (1155) |
| 1 | 9.1% (182) | 38.0% (759) |
| 2 | 18.9% (378) | 4.3% (86) |
| 3 | 26.4% (528) | 0.0% (0) |
| 4 | 22.2% (444) | 0.0% (0) |
| 5 | 13.1% (262) | 0.0% (0) |
| 6 | 5.8% (116) | 0.0% (0) |
| 7 | 2.4% (47) | 0.0% (0) |
| 8+ | 0.7% (13) | 0.0% (0) |
2,000 shuffles each. Pure random averages 3.37 same-artist pairs per shuffle, and the math for a fair shuffle says 3.38. Spread out averages 0.47.
Questions
- Is Spotify shuffle really random?
- Random, but not evenly random. Spotify used the Fisher–Yates shuffle, where every order is equally likely. Listeners complained about hearing one artist twice in a row, so in 2014 Spotify described a shuffle that spreads each artist's songs out. Its orders are still random, but clumped ones are now much rarer.
- Why does true random feel clustered?
- Because clumps are common. Over a whole playlist, the small chance that each pair of neighbours shares an artist adds up to several repeats. People notice the repeats and not the gaps, and read a pattern into them. Spotify's 2014 post calls this the gambler's fallacy.
- Is this Spotify's actual code?
- No. Spotify hasn't published its code. This is a model of the algorithm its engineering blog described in February 2014, checked against a plain Python copy on hundreds of random playlists. Spotify may have changed its shuffle since.
- What is the Fisher–Yates shuffle?
- It walks back from the last slot of the playlist. At each slot it picks one of the songs not yet placed, at random, and swaps it into that slot. Every order comes out equally likely, in a single pass.
Drill the mechanism
The spread-out shuffle starts by grouping songs by artist in a hash map. Group Anagrams drills the same move: group items by a key, then work through each group.