You run into the grocery store to quickly pick up one ingredient. You grab what you need and head to the front of the store. After quickly sizing up the check-out lines, you choose the one that looks fastest.

You chose wrong. People you could swear got in other lines long after you chose yours are already checked out and headed to the parking lot. Why does this seem to always happen to you? What kind of a cruel universe would allow such a thing to happen? It’s not fair!

Well, as it turns out, it’s just math that is working against you.

When you’re selecting among several lines at the grocery store, the odds are not in your favor. Chances are, the other line really is faster. Mathematicians who study the behavior of lines are called queueing theorists, and they’ve got the numbers to prove this. Their models also underlie a diverse set of modern problems, including traffic engineering, factory design, and internet infrastructure. At the same time, queueing theory provides a fairer way to checkout at the store. The only problem is that many customers don’t like it.

To save on labor and infrastructure, Erlang wanted to know the minimum number of lines that would be necessary to make sure that pretty much everyone’s calls could get connected. For a really cheap switchboard, you could have just one line. But then making a call would be a horrendous ordeal for customers, who would have to wait behind anyone else trying to talk at the same time. And having a line for each of the city’s thousands of telephones also doesn’t make practical sense.

Let’s look at a simplified example. If the Copenhagen switchboard has to deal with an average of two phone calls per hour, you might expect that two lines could suffice. But this fails to take into account the fact that there will be some hours when there are more calls, and some hours with less. During a very busy time, the switchboard might receive five requests for connections. With two lines, it can only provide calls for two customers, putting the rest on hold. If the Danes are particularly chatty, these calls could last an hour, meaning that more calls will arrive in the meantime, and the entire system will quickly back up.

Erlang devised equations that took into account the average number of phone calls in a given hour and the average amount of time for each call. Using his calculations in the simple example above, the Copenhagen telephone exchange would find out that they need seven lines to make sure that 99 percent of all calls would be connected right away. In 1909, Erlang published a paper with his findings, creating a new branch of mathematics called queueing theory.

Today, queueing theory finds use in many different places. Corporations with call centers, for instance, may often use the tenets of queueing theory to handle customer problems. The most basic problems, which are common, are handled by relatively unskilled but numerous representatives. More complex problems are passed up to a fewer number of people with more training. To determine the optimal number of each type of representative, a call center can use Erlang’s findings and not, as is commonly believed, a random number determined by the prince of darkness.

So back to those jerks at the supermarket who made it out ahead of you. Queueing theory explains why there’s probably no way you can always be in the fastest line. A grocery store tries to have enough employees at the checkout lines to get all their customers through with minimum delay. But sometimes, like on a Sunday afternoon, they get super busy. Because most grocery stores don’t have the physical space to add more checkout lines, their system becomes overwhelmed. Some small interruption—a price check, a particularly talkative customer—will have downstream effects, holding up the entire line behind them.

If there are three lines at the store, these delays will happen randomly at different registers. Think about the probability. The chances of your line being that fastest one are only one in three. Which means you have a two-thirds chance of not being in the fastest line. So it’s not just in your mind: Another line is probably moving faster than yours.

Now, queueing theorists have come up with a good solution to this problem: Just make all customers stand in one long snaking line, called a serpentine line, and serve each person at the front with the next available register. With three registers, this method is about three times faster on average than the more traditional approach. This is what they do at most banks, Trader Joe’s, and some fast-food places. With a serpentine line, a long delay at one register won’t unfairly punish the people who lined up behind it. Instead, it will slow everyone down a little bit.

So why don’t most places encourage serpentine lines? Here, we’re getting into customer psychology. We human beings like to think that we’re in control of our lives and can beat the system if given the chance. Researchers have noted that some customers balk at serpentine lines, which can stretch much longer than the more traditional approach, preferring their chances of winning the lottery with multiple lines.

Queueing theory has grown to more than just math, incorporating these psychological aspects to waiting in line. The reason that elevator lobbies often have floor-to-ceiling mirrors is that they help alleviate the boredom of waiting for the next lift. Lines at an amusement park like Disneyland incorporate all sorts of diversions—like changing backgrounds, progression through different rooms, and video screens—to keep park-goers preoccupied and feeling like they are making progress toward a goal while standing for two hours to get on a five-minute ride. Smart phones are probably the greatest boon to modern line designers. Pretty much anyone can now kill time by playing games, checking social media, or browsing the internet while waiting.

Finally, just to prove that the most rational choice isn’t always the best one, there’s this amusing anecdote from prominent queueing researcher Richard Larson of MIT. During a queueing theory conference, Larson said that a hotel lobby was once clogged with queueing theorists attempting to check in to their rooms. The mathematicians decided to take matters into their own hands and form a serpentine line to handle the volume.

But, as Larson told Slate in 2012: “The lobby wasn’t designed for it and it looked extremely messy. The hotel manager was unhappy. If we’d just dispersed into six parallel lines at the checkout desk the wait might have been shorter and less chaotic. But it would have been less fair.”