The Monte Carlo method: calculating by rolling dice
In 1946 the Polish mathematician Stanisław Ulam, von Neumann’s colleague at Los Alamos, was recovering from encephalitis and passing the time playing solitaire. He wondered what the probability was that a game would come out. Working it out by combinatorics was hopelessly complicated. But there was another way: play hundreds of games and count how many came out. With a computer, one could play thousands.
Ulam told von Neumann the idea, and he saw its reach at once: the same trick served for the problem occupying them at Los Alamos, the path of neutrons in a bomb or a reactor, where each neutron is deflected, absorbed or multiplied at random. Instead of solving the equations, simulate thousands of neutrons, drawing lots for what happens to each one, and take averages. Nicholas Metropolis named the method after the Monte Carlo casino, where an uncle of Ulam’s used to gamble.
In 1948 the first simulations ran on the ENIAC. Much of the code was written by Klára Dán von Neumann, von Neumann’s wife, one of the first programmers in history.
Calculating π with random points
The simplest example: draw points $(x, y)$ with $x$ and $y$ between $0$ and $1$, at random. The chance that a point falls inside the quarter circle $x^2 + y^2 \le 1$ is the ratio of the areas, $\frac{\pi/4}{1} = \frac{\pi}{4}$. So
$$\pi \approx 4 \times \frac{\text{points inside}}{\text{points drawn}}.$$The estimate improves slowly. The typical error decreases like $\frac{1}{\sqrt{n}}$: to gain one decimal place you need a hundred times as many points. For calculating $\pi$ there are much better methods. But in problems with many variables, such as the path of a neutron in three dimensions, with energy, direction and type of collision, Monte Carlo is often the only possible method, because its error does not depend on the number of dimensions.
Manufacturing chance
A computer is a deterministic machine. Where do “random” numbers come from? In 1949 von Neumann proposed the middle-square method: take a four-digit number, square it, write the result with eight digits and take the middle four. That is the next number. He knew perfectly well that this was not true randomness:
Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.
— John von Neumann, “Various techniques used in connection with random digits” (1951)
The sequence eventually repeats or falls to zero. But it was fast, and the results could be reproduced to check the calculations. Much better generators are used today, but von Neumann’s principle stands: what one wants from a generator is not true randomness, it is to pass the statistical tests the problem requires.
Exercise: the middle square
Generate three numbers by the middle-square method starting from $1234$.
$1234^2 = 1\,522\,756$, with eight digits $01522756$: the middle is $5227$. $5227^2 = 27\,321\,529$: the middle is $3215$. $3215^2 = 10\,336\,225$: the middle is $3362$. The sequence begins $1234, 5227, 3215, 3362, \ldots$
Exercise: how many points?
With $n$ points, the typical error of the estimate of $\pi$ is about $\frac{1.6}{\sqrt n}$. How many points are needed for an error of $0.01$? And of $0.001$?
$\frac{1.6}{\sqrt n} = 0.01$ gives $\sqrt n = 160$, $n = 25\,600$. For $0.001$: $\sqrt n = 1600$, $n = 2\,560\,000$. A hundred times as many points for one more decimal place, as the text says.
Sources: Nicholas Metropolis and Stanisław Ulam, “The Monte Carlo method”, Journal of the American Statistical Association (1949) · Nicholas Metropolis, “The beginning of the Monte Carlo method”, Los Alamos Science (1987) · John von Neumann, “Various techniques used in connection with random digits” (1951).
All topics on John von Neumann · ← Merge sort: sorting by joining · The biased coin that becomes fair →