Stan Ulam knew he was moving to New Mexico, but he didn’t know exactly why. Ulam was a Polish-born mathematician—and later, physicist—who first came to the United States in the late 1930s. In 1943, after Ulam had obtained American citizenship and a job at the University of Wisconsin, his colleague John von Neumann invited him to work on a secret project. All Von Neumann could reveal about the project was that it would involve relocating, along with his family, to New Mexico.

So Ulam went to the library. He checked out a book on New Mexico. Instead of skipping to the section about the state’s history or culture or climate, he turned to the opening flap, where the names of the book’s previous borrowers were listed.

This list was a curious one. It happened to include the names of fellow physicists, many of whom Ulam knew, and many of whom had mysteriously disappeared from their university posts in prior months. Ulam then cross-referenced the scientists’ names with their fields of specialty and was able to make an educated guess at the nature of the secret project.

Indeed, with World War II underway, Ulam had been invited to Los Alamos, New Mexico, to work on what would come to be known as the Manhattan Project.

The atmosphere in Los Alamos was one of collaborative fraternity. There was, indeed, something egalitarian about this whole period, at least on the surface. The Manhattan Project would come to be seen as a triumph of American ingenuity and scientific collaboration, even as it left pockmarks on the face of the earth. It destroyed cities, ended a war, and embedded the new prospect of nuclear destruction. And then, postwar America saw one of the highest rates of growth, with relatively low inequality and inflation. Marriage rates were high. World war was over, or at least on hold. It was a time of economic stability.

Ulam’s wife, Françoise said: “In retrospect I think that we were all a little light-headed from the altitude.”

It was in this postwar aftermath that Ulam would make his most important contribution to the field of optimization. He and his family decamped from Los Alamos to the University of Southern California, where in 1946 he fell ill with encephalitis. It was a difficult illness, and while Ulam recuperated in bed, he kept busy with a deck of cards and game after game of solitaire. It was in these games that an idea about optimization was born.

As he laid out cards, Ulam wondered: What are my odds of winning this round? He thought about how to calculate the odds. If he played enough times and kept track of the cards in each round, he’d have data to describe his chances of winning. He could calculate, for example, which beginning sequences were most likely to lead to a win. The more games he played, the better this data would become. And instead of actually playing a large number of games, he could run a simulation that would eventually come to approximate the distribution of all possible outcomes.

When Ulam recovered from his illness and returned to work, he began to think about applications, beyond games of solitaire, for this method of random sampling. A number of questions in physics could benefit from this style of calculation, he surmised, from the diffusion of particles to problems in cryptography. A Los Alamos colleague with whom he still corresponded, Nick Metropolis, had often heard Ulam refer to an uncle with a gambling problem. Because Ulam had conceived of the idea while playing cards, Metropolis settled on a code name, echoing the uncle’s frequent adieus as he made for the casino: “I’m going to Monte Carlo.” The method became known as the Monte Carlo method.