% test di casualità sul generatore rand
% basato sul TLC

clear;
figure(1);
n=10000;
m=20;
a=rand(m,n);
y=sum(a)/m;
ymean=sum(y)/n;
y2mean=sum(y*y')/n;
std=sqrt(y2mean-ymean^2);
sigma=sqrt(1/(12*m));
count=0;
for k=1:n
    if abs(y(k)-0.5)>2*sigma
        count=count+1
    end
end
count
pcout=count*100/n
nbin=50;
xmin=0.5-3*std;
xmax=0.5+3*std;
dx=6*sigma/nbin
x=xmin:dx:xmax;
nd=histc(y,x);
nc=nd./(n*dx);
bar(x,nc)
[string,errmsg] = sprintf('%-7.2f',pcout(1));
string1=['fuori intervallo di due \sigma :  ',string,'%'];
ymax=max(nc)
axis([x(1) x(nbin) 0 ymax*1.2]);
text(0.6*x(nbin),ymax*1.1,string1);
hold on
yg=(1/(sigma*sqrt(2*pi)))*exp(-((x-0.5+dx/2).^2)/(2*sigma^2));
plot(x,yg,'-r')
hold off