Apps & feeds

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.

Step 1 of 6 · Fair shuffle

A fair shuffle picks at random, slot by slot

Objective
Explain how Fisher–Yates fills a playlist from the back, and why every order comes out equally likely.
How this step gets you there
Step through the stage slot by slot. Each step picks a song from the ones not yet placed and swaps it in, so you see the shuffled part grow from the end.

Fisher–Yates fills the playlist from the last slot back. For each slot it picks one of the songs not yet placed, at random, and swaps it in. Every order of the 16 songs is equally likely.

Look for: The slot being filled, and the song that swaps into it.

StopPlay order
  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

1/5Copy the 16 songs in playlist order. Seed 0x3F2A.

  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D

The point: Each slot gets a song picked at random from the rest.

Next: Clumps

Step 2 of 6 · Clumps

Fair orders clump

Objective
Explain why a fair shuffle often puts the same artist back to back.
How this step gets you there
Watch the finished order and count the outlined pairs. The body ties them to one artist having many of the songs, which makes neighbours by that artist likely.

Nothing in a fair shuffle keeps an artist's songs apart. This playlist has 16 songs, 6 of them by artist A, so two songs by one artist often land side by side.

Look for: Outlined pairs of tiles: the same artist twice in a row.

StopPlay order
  1. D1
  2. B4
  3. A3
  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

1/5Slot 2: pick j at random from 0 to 2. It comes up 1, which holds B4.

  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D
Check your learning · predictWhy this question: Guessing the count shows whether you expect fairness to keep artists apart. It doesn't, and that gap is the whole reason listeners think shuffle is broken.This order is fair. How many times does the same artist play back to back?

The point: A fair shuffle plays one artist back to back often.

Next: Spread out

Step 3 of 6 · Spread out

Spotify spreads each artist out

Objective
Describe how the spread-out shuffle places an artist's songs evenly along the playlist.
How this step gets you there
Step through one artist at a time and watch their songs join the playlist. The caption gives the spacing, so you can check it against the artist's song count.

In 2014 Spotify described its fix. Group the songs by artist, then lay each artist's songs along the playlist, from 0 at the start to 1 at the end, as evenly as they fit.

Look for: Each artist's songs joining the playlist, one artist at a time.

StopPlay order
  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

1/3Group the 16 songs by artist: 4 artists. Seed 0x3F2A.

  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D
Check your learning · predictWhy this question: Working out the gap from the song count shows you see what spacing evenly means. It is what keeps one artist's songs from ending up side by side.Artist A has 6 songs. How far apart do they land?

The point: Each artist's songs are spaced evenly along the playlist.

Next: Jitter

Step 4 of 6 · Jitter

A random start and a nudge keep it random

Objective
Explain why a random start and a small nudge keep the spread-out shuffle from repeating the same pattern.
How this step gets you there
Read the start and nudge in each caption, then watch the final sort into play order. Different artists' songs interleave, but never in a fixed pattern.

Each artist's first song lands at a random point, and every song is nudged by up to a tenth of its gap, so the spacing isn't exact. Then every song plays in order of where it landed.

Look for: The start and nudge in each caption, then the final play order.

StopPlay order
  1. A6
  2. B4
  3. C2
  4. A1
  5. A4
  6. B3
  7. C1
  8. A3
  9. B2
  10. A2
  11. C3
  12. A5
  13. B1

1/3Artist C: shuffle its 3 songs and space them 1/3 apart, starting at 0.172, each nudged by up to ±0.033.

  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D

The point: Spread out stays random, and repeats get rare.

Next: 2,000 runs

Step 5 of 6 · 2,000 runs

2,000 shuffles show the pattern

Objective
Compare 2,000 shuffles of each kind and say whether pure random's clumps mean it is broken.
How this step gets you there
Watch the two histograms fill as the shuffles pile up. The final caption sets pure random's average beside the fair-shuffle math.

One shuffle is one story. This one counts same-artist pairs in 2,000 shuffles of each kind, all of this playlist, and sorts the shuffles by how many pairs they had.

Look for: Pure random's bars around 3 pairs, and Spread out's at 0 and 1.

Stophistogram

No view draws histogram frames yet.

1/41,250 of 2,000 shuffles each. Average same-artist pairs so far: 3.37 pure random, 0.48 spread out.

Check your learning · predictWhy this question: Judging the average against the math shows whether you can tell a broken shuffle from a fair one by numbers rather than by feel.Pure random averages 3.37 same-artist pairs a shuffle. Is its shuffle broken?

The point: Clumps are what fair sounds like. Spreading out makes them rare.

Next: All together

Step 6 of 6 · All together

Put it together

Here's the model whole: one playlist, shuffled pure random or spread out, beside 2,000 shuffles of each. Switch the shuffle, slide Songs by artist A, or press Shuffle again for a new seed.

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)

Go deeper

Change the spread-out shuffle

Objective
Describe how the spread-out shuffle places each artist's songs, and why that keeps repeats rare.
How this step gets you there
Step through the stage one artist at a time. Each step shows that artist's songs spaced evenly from a random start, then the final sort interleaves them all.

Here's the spread-out shuffle on a stage of its own, with Change. Give artist A more songs and step through again. On this seed it keeps every artist apart up to 7 songs by A. At 8, the same artist plays back to back twice: the more songs one artist has, the less room between them.

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

1/6Group the 16 songs by artist: 4 artists. Seed 0x3F2A.

  • CH1 Artist A
  • CH2 Artist B
  • CH3 Artist C
  • CH4 Artist D

Run it on your playlist

Objective
Check whether the clumps on your own playlist are what a fair shuffle predicts.
How this step gets you there
You type your top artists' song counts, and the model shuffles that playlist 2,000 times each way. Comparing pure random's average with the fair-shuffle math answers whether anything is broken.

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.

Practice the pattern

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.