Beneath the varnish of flashing lights and free cocktails, casinos stand on a bedrock of mathematics, engineered to slowly bleed their patrons of cash. For years, mathematically inclined minds have tried to turn the tables by harnessing their knowledge of probability and game theory to exploit weaknesses in a rigged system.

An amusing example played out when the American Physical Society held a conference in Las Vegas in 1986, and a local newspaper reportedly ran the headline “Physicists in Town, Lowest Casino Take Ever.” The story goes that the physicists knew the optimal strategy to outwit any casino game: don’t play.

Despite the warranted pessimism about beating casinos at their own games, a simple betting system based in probability will, in theory, make you money in the long run (with a huge caveat described below that I’m compelled to warn you about now, lest you read only half of this article or half of its headline).

Consider betting on red or black at the roulette table. The payout is even. (That means if you bet $1 and win, you win $1. But if you lose, you lose your $1.) And, for simplicity, assume that you really have a 50–50 shot of calling the correct color. (Real roulette tables have some additional green pockets on which you lose, giving the house a slight edge.) We’ll also suppose that the table has no maximum bet.

Here’s the strategy: Bet $1 on either color, and if you lose, double your bet and play again. Continue doubling ($1, $2, $4, $8, $16, etcetera) until you win. For example, if you lose the first two bets of $1 and $2 but win your third bet of $4, that means you lose a total of $3 but recoup it on your win—plus an additional dollar of profit. And if you first win on your fourth bet, then you lose a total of $7 ($1 + $2 + $4) but make out with a $1 profit by winning $8. This pattern continues and always nets you a dollar when you win. If $1 seems like a measly haul, you can magnify it, either by repeating the strategy afresh multiple times or by beginning with a higher initial stake. If you start with $1,000 and double to $2,000, and so on, then you will win $1,000.

You might object that this strategy only makes money if you eventually call the right color in roulette, whereas I promised guaranteed profit. The chance that your color hits at some point in the long run, however, is, well, 100 percent. That is to say, the probability that you lose every bet goes to zero as the number of rounds increases. This even holds in the more realistic setting where the house enjoys a consistent edge. If there is some chance that you win, then you will win eventually because the ball can’t land in the wrong color forever.

So should we all empty our piggy banks and road-trip to Reno, Nev.? Unfortunately, no. This strategy, called the martingale betting system, was particularly popular in 18th-century Europe, and it still draws in bettors with its simplicity and promise of riches, but it is flawed. Gambling ranked among the many vices of notorious lothario Jacques Casanova de Seingalt, and in his memoirs he wrote, “I still played on the martingale, but with such bad luck that I was soon left without a sequin.”

Do you spot a flaw in the reasoning above that promised profits? Say you have $7 in your pocket, and you’d like to turn it into $8. You can afford to lose the first three bets in a row of $1, $2 and $4. It’s not so likely that you lose three in a row, though, because the probability is only one in eight. So one eighth (or 12.5 percent) of the time, you lose all $7, and the remaining seven eighths of the time, you gain $1. These outcomes cancel each other out: –1/8 x $7 + 7/8 x $1 = $0. This effect scales up to any amount of starting capital: there is a large chance of gaining a little bit of money and a small chance of losing all of your money. So while many gamblers will turn a small profit playing the martingale system, the rare gambler will suffer complete losses. These forces balance out so that if a lot of players used the strategy, their many small winnings and few huge losses would average out to $0.

But the true argument doesn’t stop at $7. I said the idea is to keep playing until you win. If you lose three in a row, go to the ATM and bet $8 on a fresh spin. The guaranteed profit comes from a willingness to keep betting more and the inevitability of winning at some point with persistent play. Here’s the key defect (which is hard to hear): you only have so much money. The amount you wager each round grows exponentially, and it doesn’t take long before you’re betting the farm just to make up your losses. It’s a bad strategy for generating wealth when you’re taking a small but nonzero chance of risking your livelihood for a puny dollar. Eventually you’ll go bankrupt, and if this happens before your jackpot, then you’re out of luck. Finitude breaks the martingale in another way, too. Probability dictates that you are guaranteed to win eventually, but even if you had a bottomless purse, you could die before “eventually” arrives. Yet again, the pesky practicalities of the real world meddle with our idealized fun.

Reflecting back, it may seem obvious that you can’t actually force an advantage in a casino game. Yet it is surprising that we have to resort to arguments about solvency and mortality to rule it out and that the dreamy pencil-paper world that mathematicians inhabit, where infinity can roam free, actually permits what should be impossible.

For games with 50 percent or worse winning chances, there is no betting strategy that secures an upper hand in a finite world. What about more favorable games? If you had $25 in your wallet and could repeatedly bet on the outcome of a biased coin that you knew turned up heads 60 percent of the time (again, where you either lose your full bet or gain an amount equal to it), how much money could you turn your $25 into in a half hour? Researchers tested 61 finance students and young professionals with this exact experiment and were surprised by their poor performance. (You can try it for yourself.)

A disconcerting 28 percent of participants went broke despite having an advantage, and a shocking two thirds bet on tails at some point in the game, which is never rational. On average, the participants walked away with $91 (winnings were capped at $250). This may seem like an ample take when starting with $25, but the researchers calculated that, over the 300 coin tosses time allowed for, the average winnings of players using the optimal strategy (described below) would be more than $3 million!

The players face a dilemma: Bet too much per round, and they risk losing their whole bankroll on a few unlucky tosses. But bet too little, and they fail to capitalize on the sizable advantage the biased coin affords them. The Kelly criterion is a formula that balances these rival forces and maximizes wealth in such situations. Scientist John Kelly, Jr., who worked at Bell Labs, realized that to make the most money, a gambler should bet a consistent fraction of their purse on every round. He worked out a simple formula for the perfect fraction: 2p – 1, where p is the probability that you win (p = 0.6 in the running example), which he described in a 1956 paper. In the experiment, betting 20 percent of your available cash on each flip hits the sweet spot. Note that the strategy puts more money on the line if you keep winning, and it constricts bet size as your cash dwindles, making it very unlikely to go bust.

Unlike the martingale betting strategy, the Kelly criterion works in practice and proves its worth as a mainstay of quantitative finance. Professional card counters in blackjack also use it to size their bets when the odds are goodq. Economists warn that while the Kelly criterion thrives at generating wealth, it’s still a gamble with pitfalls of its own. For one, it assumes that you know your probability of winning a bet, which works in many casino games but less so in fuzzy domains, such as the stock market. Also, Kelly asserts that in the experiment above, you’re most likely to grow your wealth if you keep betting 20 percent of it. But if you have $1 million to your name, it’s perfectly reasonable not to want to gamble $200,000 on a coin flip. At some point, you need to price in your personal level of risk aversion and adjust your fiscal decisions to respect your own preferences.

Still, if you find yourself placing wagers with odds in your favor, ditch the martingale and remember that the Kelly criterion is a better bet.

This is an opinion and analysis article, and the views expressed by the author or authors are not necessarily those of Scientific American.