In this simulation, we consider a box divided in half with by a membrane with a small hole in the center. The density of the gas is small enough so that the gas molecules do not interact. The total number of particles in the box is N, and the number on one side starts at n (leaving N-n on the other side).

This simulation implements the ApproachToEquilibriumParticlesInBox() algorithm described on pages 9-10 of Statistical and Thermal Physics by Gould and Tobochnik.

In [1]:

```
from __future__ import division #crucial to do this first!
```

In [7]:

```
def propagate(n,N):
r = random.random()
if r < n/N and n > 0:
n = n - 1
elif r >= n/N and n>=0:
n = n + 1
elif r < n/N and n == 0:
n = n
else:
print "problem: should not have arrived here!"
pass
return n
def createRun(n, N, timeSteps):
t = np.arange(0,timeSteps)
left = []
right =[]
for i in range(len(t)):
n = propagate(n, N)
left.append(n)
right.append(N-n)
return np.array(left), np.array(right)
```

In [18]:

```
def runAndPlot(nleft, N, timeSteps, numTrials):
for i in range(numTrials):
left, right = createRun(nLeft, N, timeSteps)
if i==0:
numLeft = left
numRight = right
if i >= 0:
numLeft = np.vstack((numLeft, left))
numRight = np.vstack((numRight, right))
figsize(10,8)
xlabel("Number of Steps")
ylabel("Number on Left Half of Box")
ylim(0,N)
plot(np.average(numLeft,axis=0))
```

Here are three differnet runs with 1, 10, and 1000 trials all displayed together:

In [19]:

```
runAndPlot(1,10,100,1)
runAndPlot(1,10,100,10)
runAndPlot(1,10,100,1000)
```

In [ ]:

```
```