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.

StopSeed0x3F2ASongs16
  1. D1
  2. A3
  3. B4
  4. A1
  5. B3
  6. B1
  7. A4
  8. A6
  9. D2
  10. C1
  11. C2
  12. A2
  13. D3
  14. B2
  15. A5
  16. C3
  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D
Same artist back-to-back3

fisherYates() on 16 songs, seed 0x3F2A. Done: the same artist plays back to back 3 times. Outlined pairs are the repeats.

Shuffle
New seed
2,000 shuffles of this playlistPure random avg 3.37Spread out avg 0.47
Table
2,000 shuffles of this playlist
Same artist back to back, per shufflePure randomSpread out
01.5% (30)57.8% (1155)
19.1% (182)38.0% (759)
218.9% (378)4.3% (86)
326.4% (528)0.0% (0)
422.2% (444)0.0% (0)
513.1% (262)0.0% (0)
65.8% (116)0.0% (0)
72.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

StopPlay order
Songs by artist A
  1. A1
  2. A2
  3. A3
  4. A4
  5. A5
  6. A6
  7. B1
  8. B2
  9. B3
  10. B4
  11. C1
  12. C2
  13. C3
  14. D1
  15. D2
  16. D3
  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D
Group the 16 songs by artist: 4 artists. Seed 0x3F2A.

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.

Songs per artistUp to 4 artists, 1 to 12 songs each. Tap to edit.
StopFair shuffle, by the mathavg 3.38
2,000 shuffles of your playlistPure random avg 3.37Spread out avg 0.47
Table
2,000 shuffles of your playlist
Same artist back to back, per shufflePure randomSpread out
01.5% (30)57.8% (1155)
19.1% (182)38.0% (759)
218.9% (378)4.3% (86)
326.4% (528)0.0% (0)
422.2% (444)0.0% (0)
513.1% (262)0.0% (0)
65.8% (116)0.0% (0)
72.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.