How a mathematical magician revealed a trick to beat the casino house.

The company executives were anxious. It manufactured precision card shuffling machines for casinos.
Thousands of its mechanical shufflers were in operation in Las Vegas, in the United States, and throughout the rest of the world. Rental fees yielded millions of dollars annually, and the company's shares were traded on the New York Stock Exchange.
But the executives had recently discovered that one of their machines had been "hacked" by a gang of opportunists. They were using a video camera hidden behind a window to record the operation of the card shuffler.
The images, transmitted to an accomplice in the casino parking lot, were played in slow motion to identify the sequence of cards in the deck, which was then relayed to players inside. The casino lost millions of dollars before the gang was finally discovered.
The executives were determined not to be hacked again and developed a prototype of a new, sophisticated card shuffling machine, this time enclosed in an opaque box.
Their engineers guaranteed that the machine would shuffle the cards efficiently in just one pass through the device, reducing the time humans spend handling the deck and also making it more difficult for dishonest card counters and croupiers to operate.
But they needed to make sure their machine shuffled the cards properly. For that, they needed Persi Diaconis.
Diaconis, a magician who graduated in mathematics from Stanford University in the United States, is considered the world's leading expert in the mathematics of card shuffling. Throughout the academic literature on the subject (which is surprisingly extensive), his name stands out like the ace of spades in a magic trick.
Therefore, when the company executives contacted him and offered to show him the inner workings of the machine – a veritable “black box” – he could hardly believe his luck.
With his collaborator Susan Holmes, a statistician from Stanford, Diaconis traveled to the company's showroom in Las Vegas to examine a prototype of the new machine. And the pair soon discovered a flaw.
Although the mechanical shuffling action appeared random, mathematicians observed that the result still had ascending and descending sequences, which meant that it was still possible to make predictions about the order of the cards.
To prove this to the company executives, Diaconis and Holmes devised a simple technique to guess which card would be revealed next.
Let's say the first card revealed was the five of hearts. Your guess would be that the next card was the six of hearts, assuming the sequence was increasing. If the next card was actually lower – the four of hearts, for example – the sequence was descending, and your next guess would be the three of hearts.
With this simple strategy, mathematicians were able to guess nine or ten cards per deck – a fifth of the total, enough to double or triple the advantage of a skilled card counter.
Card counting is a practice in which the player keeps track of which cards have been played, in order to have a slight advantage in predicting the probability of the next card being good or bad.
This practice has existed for decades (and, in some games, like bridge, is a legitimate part of the game), but it is heavily repressed in casino games, such as blackjack or twenty-one. And it is illegal to use technology to assist a card counter.
The executives were horrified. “We are not satisfied with your conclusions,” they wrote to Diaconis, “but we believe them, and that’s why we hired you.” The company quietly shelved the prototype and turned its attention to another machine.
Magic and mathematics
Diaconis spent his entire life studying problems at the boundaries between order and randomness – whether decoding encrypted messages, piecing together DNA strands, or optimizing web search engines.
His interest in cards began with a chance encounter in 1958. At age 13, at the traditional Tannen's Magic Emporium in Times Square, New York City, Diaconis met Alex Elmsley, a quiet-spoken Scottish magician and computer scientist who had mastered "perfect shuffling."
Sometimes called the "pharaoh's deck" or simply "the technique," the perfect shuffle consists of cutting a deck of cards into two piles of exactly 26 cards each and perfectly alternating the cards as if it were a zipper, alternating one card from each hand.
Very few people can do this correctly in under 10 seconds. Diaconis is one of them.
The perfect shuffle has been used by gamblers and magicians for centuries because it gives the illusion that the cards are shuffled randomly. But it's far from random, in fact. If you perform the same sequence of perfect shuffles eight times in a row, the deck will magically return to its original order.
Diaconis likes to demonstrate the perfect shuffle by taking a new deck of cards and writing the word "RANDOM" on one side of the cards with a black marker.
As he performs his card trick, the letters become jumbled. Sometimes they appear in ghostly form, like a poorly tuned image on an old TV set.
But after he shuffles for the eighth and final time, the word rematerializes next to the deck. The cards are in exactly their original sequence.
The mathematics of cards
Back at Tannen's Magic Emporium, Elmsley explained the subtle mathematics behind the trick. Imagine you number a new deck of cards from 1 to 52, where 1 is the top card of the deck and 52 is the bottom card.
When you perform the perfect shuffle, the cards move to new positions in the deck. The card that was originally in position 2, for example, will move to position 3; the card in position 3 will go to position 5… the card in position 27 will return to position 2, and so on.
The perfect shuffle can be considered a complete series of cycles, like musical chairs in separate games. The number of times it takes for the cards to return to their correct order is the least common multiple of the extensions of all the cycles: in this case, eight (since 8 is the least common multiple of 1, 2, and 8).
The year after meeting Elmsley at Tannen's Magic Emporium, Diaconis left home at the age of 14 to learn magic under the tutelage of a famous magician. They spent 10 years on the road, learning every possible style of shuffling cards and observing dishonest croupiers to learn their techniques.
But the conversation with Elmsley had piqued Diaconis's curiosity. What other connections might there be between mathematics and magic?
Diaconis claims that his tombstone will bear the inscription "seven shuffles are enough." He is referring to his most famous discovery: seven "quick shuffles" are needed to create a sufficiently random deck of cards.
Quick shuffling is a well-known technique, used in casinos and by serious card players, in which the deck is cut in two and the cards are pushed with the thumb so that they become satisfactorily intertwined, often ending with a bridge that brings the cards together, tidying up the pile.
Quick shuffling is the twin brother of perfect shuffling. Instead of perfectly interleaving the two halves of the deck, the halves are mixed together in disordered groups, planting a seed of randomness that progressively mixes the cards each time they are shuffled.
After one or two quick shuffles, some cards will remain in their original sequence. Even after four or five shuffles (far more than usual in most casinos), the deck will retain some semblance of order. But when you shuffle seven times, the cards become truly mixed up, at least according to most statistical tests.
Beyond that point, mixing more won't yield significant results. "It's the closest you can get to randomness," according to Diaconis.
To rigorously study the shuffling, Diaconis used a mathematical tool known as a Markov chain.
“A Markov chain is any repeated action whose outcome depends only on the current state and not on how that state was reached,” explains mathematician Sami Hayes Assaf of the University of Southern California in the United States.
This means that Markov chains have no "memory" of what came before. It's a very good model for shuffling cards, according to Assaf. The result of the seventh shuffle depends only on the cards after the sixth operation and not on how the deck changed in the previous five shuffles.
Markov chains are widely used in statistics and computer science to manipulate sequences of random events, whether they are shuffles of cards, vibrating atoms, or fluctuations in stock prices. In each case, the future “state”—the order of the cards, the energy of the atom, or the value of the stock—depends only on what is happening now and not on what happened before.
Despite their simplicity, Markov chains can be used to make predictions about the probability of certain events after multiple repetitions. Google's PageRank algorithm evaluates websites in its search results based on a Markov chain, which models the behavior of billions of internet users who randomly click on links on the web.
Working with mathematician Dave Bayer of Columbia University in New York, Diaconis demonstrated that the Markov chain describing rapid shuffles exhibits a strong transition from ordered to random after seven shuffles. This behavior, known to mathematicians as the cutoff phenomenon, is a common feature of problems involving mixtures.
Imagine you're stirring cream into coffee. As you stir, the cream forms thin white streaks in the black coffee, until they are suddenly and irreversibly mixed together.
Knowing which side of the cutoff the deck is on – whether it has been shuffled properly or still retains some memory of its original order – gives players a distinct advantage against the dealer.
'To think is to think'
In the 1990s, a group of students from Harvard and MIT (the Massachusetts Institute of Technology, USA) managed to beat the odds playing blackjack in casinos across the United States by counting cards and using other methods to check if the deck was properly shuffled.
Casinos responded by introducing more sophisticated card shuffling machines and shuffling the cards before they were fully in play, as well as increasing player surveillance. But it is still rare to see cards being shuffled by machines the seven times required in a casino.
Casino executives may not have paid much attention to Diaconis and his research, but he continues to have enormous influence on mathematicians, statisticians, and computer scientists who study randomness.
At a conference in Stanford in January 2020 to commemorate Diaconis' 75th birthday, colleagues from around the world gave talks on topics such as the mathematics of genetic classification, how cereal settles in a box when shaken, and, of course, shuffling cards.
Diaconis doesn't care much about gambling. He says there are better and more interesting ways to make a living. But he doesn't resent gamblers who try to gain an advantage by using their brains.
“Thinking is not cheating,” he says. “Thinking is thinking.”
(Shane Keating is a science writer and professor of mathematics and oceanography at the University of New South Wales in Sydney, Australia. ( UOL News – Shane Keating – BBC Future – Read the original version of this report ( in English ) on the BBC Future website.)
***
Who cares about the math behind card tricks?
Among the works published in the 1990s, Persi Diaconis proved several rigorous mathematical rules about shuffling cards.

Stanford University researcher Persi Diaconis interrupted his presentation at the event commemorating the 70th anniversary of IMPA (Institute for Pure and Applied Mathematics) two weeks ago with a question rarely heard at a research conference: "Who cares about this?"
Diaconis is a unique figure in the world of mathematics, with an unusual personal and professional trajectory. I first met him in 1998, when we both gave plenary lectures at the International Congress of Mathematicians in Berlin.
We met again in person 15 years later, when he gave a popularization lecture on the mathematics of coincidences at the 1st Congress of Mathematics of the Americas, which I helped organize in the Mexican city of Guanajuato.
Born in 1945, at the age of 14 Diaconis left home to travel with the magician and prestidigitator Dai Vernon, alongside whom he performed in circus shows. At the time, he dropped out of school, of course. But he returned ten years later, determined to learn the mathematics behind the card tricks he had discovered in his travels.
He graduated from the City University of New York in 1971 and received his doctorate in mathematical statistics from Harvard University three years later. It is said that during this period he supported himself and paid for his studies by playing poker on ships that connected New York to the southern United States.
Today he is a near-legendary leader in the world of statistical research, in addition to continuing to be an exceptional magician.
In some of his most famous works, published in the 1990s, Diaconis proved several rigorous mathematical rules about shuffling, and what needs to be done for the order of the cards to be considered genuinely random.
“I’m sure the rule that you only need to shuffle seven times will be written on my tombstone!” he joked in a conversation. That was precisely the topic of his lecture at IMPA, by the way. And he himself answered his own question about who cares about that.
“Well, a lot of people play cards, and the people who shuffle the deck care about how many times they should shuffle the cards. Basically, they changed the rules in Las Vegas because of a theorem that we proved!” This story deserves to be told, but it will have to wait until next week. ( Folha de S.Paulo – Marcelo Viana – Director-General of the Institute of Pure and Applied Mathematics, winner of the Louis D. Prize, Institut de France)


