%% author: Tim Ratigan
%% description: run a simulation of an Ehrenfest model after N steps, with
%%              M trials
%% usage: modify M,N, run.


N = 100000; %% number of steps
M = 10000; %% number of trials

count = zeros(M,1); %% maintains the ball count in A for each trial

for i = 1:M  %% run simulation M times
    balls = zeros(1000,1);
    for j = 1:N  %% change a random ball's bin N times
        ball = floor(1000*rand) + 1;
        balls(ball) = 1 - balls(ball);
    end
    count(i) = 1000-sum(balls); %% total balls in A
end

histogram(count,min(count):max(count)); %% plot results