TITLE: Mathematical reason behind exponential distribution in random shuffling of balls in boxes
QUESTION [7 upvotes]: Let's take a very simple problem where 1000 identical boxes initially each have 10 balls. Then we select at random one box, take out a ball from it and put it into another box chosen again randomly.
This algorithm repeated large number of times always give exponential distribution with most boxes being empty and only few with large number of balls.
Sample code for problem illustration:
import numpy as np  
import matplotlib.pyplot as plt  

box = np.ones(1000)  
box = box*10  
for i in range(100000):  
    rand = np.random.randint(low=0, high=1000, size=2)  
    if box[rand[0]] > 0:  
        box[rand[0]] = box[rand[0]] - 1  
        box[rand[1]] = box[rand[1]] + 1  
#     print(box)  
plt.hist(box,bins=50)  
plt.show()


Question:  
Probability of choosing a box is equal for any box, then can't we assume that in large number of trials, each box is chosen equal number of times for removal of ball and similarly each box is chosen equal number of times to add a ball to it?
Why after a large repeated trials, the number of balls in each box does not reach equilibrium value 10. Why most of the boxes are emptied and only handful are filled with huge number of balls. This seems counter-intuitive.
Is it something to do with the way random number generators are designed so we indeed get binomial distribution to imitate some physical behavior? Or there is a purely mathematical intuition behind?

REPLY [3 votes]: I will assume that the number of boxes is very large -- call it $n$ -- and the number of random steps in the experiment is much larger, such that we have reached whichever kind of steady state the experiment tends towards.
Intuitively, in the steady state a certain number of boxes will be non-empty at any given time. This number can fluctuate up and down a bit, but I will make the somewhat handwawy assumption that it stays near some limit mos of the time. Let's assume that the "usual" number of non-empty boxes is $kn$ for some constant $k\in(0,1)$.
Let's look at how the number of balls in any particular non-empty box evolves. At a given point in time there are $n-1$ different random pairs that will lead to the number decreasing by one, but only $kn-1$ different random pairs that will lead to the number increasing. So the probability that the next change to this particular box will be an increase is $\frac{kn-1}{n-1+kn-1}$. Since we're assuming $n$ to be large, we can ignore the $-1$s (they stand for the box being picked as both the "from" and "to" box at the same time), and say that the probability is $\frac{k}{1+k}$.
Thus, over a long time the number of balls in one single box will behave like a random walk that moves up with probability $\frac{k}{1+k}$ and down with probability $\frac{1}{1+k}$. There's a good theory of such random walks, and it says that the number of balls we expect to find in the is geometrically distributed exactly like you have observed. (That is, except that it only behaves according to the nice theory as long as the number is at least $1$. When it drops to $0$, the next step in the random wall is necessarily "up", but the waiting time until that step happens is different from the usual expectation, which the simple analysis so far doesn't take into account -- note, tough, that @zoli's answer contains the germ of a simple proof that the pattern does continue all the way to $0$).
In other words, as far as I can see, the system has no inherent dynamic that looks like "big boxes tend to stay big" -- all non-empty boxes shrink probabilistically at the same rate, and the few big ones you see are just flukes that have temporarily acquired a lot of balls.
The outcome of the random-walk calculation is very nice: it says that if the steady state has a certain amount of boxes with $m\ge 1$ balls, there will be $k$ times as many boxes with $m+1$ balls. (There is some handwaving going on here, pretending the counts of balls in different boxes are independent even though they're not really -- I'll gleefully sweep all that under the carpet, in the interest of getting a qualitative understanding for large $n$). We can use this to derive what $k$ must be. Namely, suppose that there are $\ell n$ boxes with one ball in them. Then the total number of live boxes is
$$ kn = \ell n  + k \ell n + k^2 \ell n + k^3 \ell n + \cdots = \frac{\ell n}{1-k} $$
which we can easily solve to get
$$ \ell = k(1-k) $$
We can also count the total number of balls:
$$ \ell n + 2 k \ell n + 3 k^2 \ell n + \cdots = \frac{\ell n}{(k-1)^2} = n\frac{k}{1-k} $$
Since we know there are really ten times as many balls as there are boxes, we can use this to find $k$:
$$ \frac{k}{1-k} = 10 ~\iff~ k = \frac{10}{11} = 0.909090\ldots$$

Let's check that this looks like a steady state. What is the probability that a random move will increase the number of empty boxes? It needs to move a ball from a size-1 box to a non-empty box other than itself), so the number of pairs that do this is
$$ \ell n \cdot kn - 1 = k^2(1-k) n^2 -1 $$
On the other hand, what is the chance to decrease decrease the number of empty boxes. For this we need to move a ball from a box of size $\ge 2$ to a previously empty box. The number of pairs that do this is
$$ (kn - \ell n) \cdot n - kn = (k - k(1-k))(1-k)n^2 = k^2(1-k) n^2 $$
That is, exactly the same, other than the $-1$ that we're ignoring because $n$ is large.