% test of the central limit theorem with the uniform distribution

N = 50; % number of samples to draw

K = 5000;  % number of trials
 
x = sum(rand(N,K))/N;  % draw N uniformly distributed number and find their average 

mn =  0.5  %The theoretical mean
vr = 1/12;  % The theoretical variance
xm = max(abs(x-mn));  % gives a range for the histogram
dx = 2*xm/30;  % number of grid points for the histogram
xv = [mn-xm:dx:mn+xm];  % range for the histogram
h = hist(x,xv)/(K*dx);  % normalized histogram
figure(1)
bar(xv,h)
hold on
y = [0:.01:1];
f = sqrt(N)* exp(-N*(y-mn).^2/(2*vr))/(sqrt(2*pi*vr));  % normal distribution
 
plot(y,f,'--','linewidth',2)

hold off

 