% check the binomial theorem approximation 
set(0,                           ...
   'defaultaxesfontsize', 20,   ...
   'defaultaxeslinewidth', 1.0, ...
   'defaultlinelinewidth', 1.2, ...
   'defaultpatchlinewidth', 0.7);

%set N must be an even number

nlist = [10,100, 500,1000, 5000,10000];

M=5000; % the number of trials
for j = 1:length(nlist)
N = nlist(j);
R = randi(2,N,M)-1;
Rs = sum(R);
 
Nby2 = sum(Rs==N/2);
p(j)=Nby2/M;  % this is the data
p_est(j) = sqrt(2/(pi*N));  % this is the binomial theorem estimate
end

% make a log-log plot
loglog(nlist,p_est,'--',nlist,p,'*','linewidth',2)
