Many people think of gambling as a frivolous entertainment at best, or a corrupting sin at worst. But throughout history, there have been mathematicians and scientists who have seen the games you’d confront in a Las Vegas casino a very different way — as a playground for ideas where they can test out different notions of how the world works.

That’s the premise of a new book, “The Perfect Bet: How Science and Math Are Taking the Luck Out of Gambling.” In the book, mathematician Adam Kucharski traces the long, tangled relationship of betting and science, from the origins of probability theory over a dice game to the kind of sophisticated counting techniques that have won MIT graduates millions in Vegas.

In an interview, Kucharski said he has always been obsessed with the puzzles of casino games. But when he was a PhD student, he started getting recruited by betting hedge funds, which were earning big profits for their investors by putting wagers on such events as the outcomes of soccer games.

That piqued his interest and triggered a deep dive into the interconnected history of science, math and gambling. He found that Johannes Kepler, Galileo Galilei, Alan Turing and many other key scientists studied gambling, and these studies gave rise to many of the scientific ideas we use today, including modern statistics, game theory and chaos theory.

I spoke with Kucharski about his book and the fascinating and important things humans have learned from spinning roulette wheels and racing horses. This interview has been edited for length and clarity.

You have so many stories in the book of these amazing successes, where mathematicians and statisticians triumph over casinos and games. What was one of the most lucrative strategies in history you found?

One of the stories I really liked was about students at MIT, who started thinking about lotteries as part of a math project in 2005. Generally, you’d expect to lose money in lotteries, because that’s how they work. But as they expanded their analysis, they found a lottery that had been introduced fairly recently in Massachusetts called Cash WinFall that had this specific property. If the jackpot reached a certain limit and nobody won it, the prizes would roll down, meaning they would go to people who matched fewer numbers.

In these weeks where you had this roll-down feature being triggered, it could be quite profitable. The MIT students realized that if you bought enough tickets in the right combinations of numbers, you could pretty much guarantee a profit.

Firstly, it’s a great story because it starts with this innocuous college project and then grows into something where they incorporate a company to do this systematically. A number of syndicates also were getting involved as well, because essentially this had become the most lucrative lottery in the United States. But then there was one week when the MIT group actually bought up enough tickets to trigger the roll-down. They realized that if they bought enough tickets to raise the jackpot to \$2 million, they could force the lottery to roll down immediately, while the other syndicates were waiting for it to occur two or three weeks later.

It’s amazing how many of these strategies, from blackjack, and roulette as well, started essentially with math and physics students who were looking for loopholes and chipping away at existing systems and trying to find gaps in them. Growing up, we’re taught that lotteries can’t be beaten and roulette’s a game of chance. But when people find ways of proving that wrong, it can be quite lucrative.

I can definitely see this book inspiring some math and physics students to try to crack more of these games. But for the average person, is this really a way to get rich quick? First you need to get a PhD in math or statistics.

It’s incredibly difficult to get rich quick. Even these very successful strategies require a lot of hard work and focus as well as really innovative ideas.

But one of the things that came out of these stories for me is the benefit that you can get from thinking about the world in this way. Even if you’re not a gambler, even if you don’t go to casinos, you’re going to have to face risk and uncertain situations and make decisions when you don’t have the full information available. Gambling is almost a summary of those problems as they’ve been faced by scientists in the past. By looking at these stories, we can learn a lot about how to make decisions and how to distinguish between luck and skill, which in many cases we don’t always get right.

What has science learned from gambling?

My day job is in public health, looking at disease outbreaks. Many of the methods we use originated with games and gambling. All the concepts around probability, how we measure the chance of an event, were only developed in the 16th century with people studying dice games. The concepts of statistical theory and testing a hypothesis were also inspired by dice games and roulette just over 100 years ago.

Games also gave rise to some more modern computational techniques. In the late 1940s, a mathematician was looking at a form of solitaire and wanting to know how the cards might fall. He wasn’t a big fan of pen-and-paper calculations, so he decided to lay it out and see what happened. He realized that if you have these complicated probability questions, you can simulate lots of random outcomes, and then you get a sense of what patterns you might see. Today, we call that the Monte Carlo method, and we use it to simulate the outcome of things like epidemics.

Simple games, because they’re nice situations to look at mathematically, have turned out to be quite important to wider areas of science.

Most of the successful betting strategies you talk about in your book were actually developed by academics and scientists, not by professional gamblers. And often, those scientists didn’t really go on to reap the winnings from their ideas. Why is that?

One of the main reasons is because of dogma and existing beliefs. For a long time, scientific approaches were not believed to be useful in casino games, and often, it took an outsider to come up with new ideas. A good example is blackjack. Edward Thorp, who pioneered the card-counting strategies people use to play blackjack today, was laughed at the first time he went in a casino because he was playing with tactics that just seemed so absurd to people at the time.

It is surprising how many of these people didn’t go on to be professional gamblers. I think that’s because, for them, that wasn’t the end game. Gambling was a way of testing out these skills and concepts that are incredibly important in their day jobs. If you’re a mathematician or you work in any industry that involves quantitative analysis or the ability to take data and convert it into a prediction about an event, betting is a really good way of refining those skills.

What have you learned about the mistakes that people often make when trying to predict the outcome of games? For example, in the section in the book on horse racing, you mention that people often gamble too much on underdogs. Are there other common errors?

There’s a number of biases we fall into. One is the “favorite-long-shot bias.” In horse racing, if you look at the horses that are in the back of the pack, they have higher odds than their performance suggests they should. In other words, people overestimate the chance of long shots winning. That feature also pops up in other sports, and even in how we predict weather or severe political events. People tend to focus on things that are surprising and overestimate the chances of unlikely events.

Another is known as the Monte Carlo fallacy. This originated in roulette, where when the same color comes up multiple times, people tend to start piling money on the other color. They think that if black has come up a lot, then red must be due. Of course, it isn’t, because the result is still completely random, but there’s this psychological bias dragging us one way.

A third psychological quirk which pops up a lot in games is what’s known as gambler’s ruin. This is the tendency where if people win at a game, they increase the amount of money they’re risking. But often when people lose, they don’t decrease the amount that they’re risking. Mathematically, this will always lead you to bankruptcy. This is why bankroll management is incredibly important, because you need to resist this temptation and adjust the amount you’re risking depending on where you are in the game.