Technology

How random is random on your music player?

Random logo Image copyright Thinkstock

There I was, walking to the railway station, minding my own business, when... bam!

Our song.

That's the gamble you take when you set your device to "random". Sometimes you get good songs, sometimes you get bad ones. Sometimes you get songs that remind you of someone that was once in your life.

I skipped it. Buddy Holly saved my mood, and saved the day.

Until home time. Hopping on to the Tube, press play. It was back. Our song.

Twice in one day? What did it mean? What was the universe trying to tell me? It was almost Valentine's Day - should I get back in touch?

Don't be daft.

Skip.

Chin-strokers

When we attach so much of our own emotion, be it a place, memory or person, into the songs we cherish, eerie coincidences brought up by shuffle or random functions can be unsettling.

Or in some cases just irritating. Five thousand songs, and it brings up three tracks by the same artist in quick succession? Come on now, that can't be right. This is supposed to be random!

Except, of course, it is random. Our brains just don't like it.

At the music-streaming service Spotify, developers have applied their finest chin-strokers to the issue. Users were complaining, they said, that their shuffle function was not random at all.

"Our brain is an excellent pattern-matching device," said Babar Zafar, a lead developer at Spotify, in an interview for Tech Tent on the BBC World Service.

"It will find patterns where there aren't any."

Image copyright Thinkstock

Users were complaining in their droves, he said, that Spotify was somehow forcing music upon them. Conspiracies included the idea that record labels had struck up some kind of deal to make sure some artists are heard more than others. Mr Zafar dismissed that thought as hogwash.

"The problem is that, to humans, truly random does not feel random," said Mattias Johansson, a Spotify software engineer, in a response on the question-and-answer site Quora.

"So we got tons of complaints from users about it not being random.

"Last year, we updated it with a new algorithm that is intended to feel more random to a human."

Coincidental jazz

The fact is that the human brain, for all its brilliance, is a sucker for coincidences. And more importantly, given the topic of this article, the brain is also keen to believe that something not happening for a long time makes it more likely to happen next time.

The theory is known as the Gambler's Fallacy or the Monte Carlo Fallacy, owing to one memorable night in 1913 when the roulette ball fell on black 26 times in a row.

Players at the table were betting huge amounts in disbelief, expecting the normal "random" order of the world to be set right with each spin.

The very foundations of the billion-dollar casinos of Las Vegas are built on the fallacy. There is a pernickety bit of our brain that thinks if we did not win the last five hands of blackjack we are surely going to get lucky when the next hand is dealt.

Image copyright Getty Images
Image caption Las Vegas is built on the Gambler's Fallacy, the idea things will, surely, turn in our favour

When applied to how songs pop up on our music players, the fallacy means we, for some reason, think it less random when two similar songs play.

It is a lack of uniform distribution that throws us off, writes Martin Fiedler - a programmer who describes himself as an "average nerd".

Consider a playlist of 32 songs, he says, split almost evenly into three genres - 10 of genre A and 11 each of genre B and C.

A truly random playlist might spit out something like this: AACBBCBACABBCCACCCCABBACBACABABB

It is random, but it sure will not feel like it - and that block of four Cs in the middle there might be annoying. Particularly if they are jazz.

"This example, short as it is, already exhibits the two main problems of random shuffle algorithms," writes Mr Fielder.

"The first one is the burst of four adjacent C's in the middle of the sequence, the second one is the lack of B's in that area (there's no B for 8 slots, which is quarter of the whole sequence!)."

Music in uniform

So here is how Spotify solved the problem.

What our brains actually crave from "random" is that the three genres get spread nicely over the playlist.

Which, after a lot of mathematical magic, is what Spotify does now.

"Suppose we have a playlist containing some songs by The White Stripes, The xx, Bonobo, Britney Spears and Jaga Jazzist.

"For each artist we take their songs and try to stretch them as evenly as possible along the whole playlist."

Spotify's algorithm takes into account how long a playlist is, and how many of each type of song there are. So if there are four White Stripes songs in the list, they will each appear at roughly 25% intervals.

There is no chaos here - instead a highly structured and sophisticated way to trick your brain into thinking it is getting a great mix of shuffled tracks... that may or may not remind you of your ex.

Skip.

Follow Dave Lee on Twitter @DaveLeeBBC

More on this story