
close all
clear all


 disp('Is RAND''s 1000000 Random Numbers Random?')
 lw = 2.5; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 15;
 msize = 10;




load 'C:\BESTAT\Goodness\Goodnessdat\RAND1Millrandomdigits.txt'
% the first column is the number and next 10 columns are random digits
% of length 5 (random numbers 0:99999) set in 20000 rows.
x = RAND1Millrandomdigits(:,2:11);
xx = x(:); %make it a vector
[fre o]=hist(xx, 100);
hist(xx, 100);%plot
chi2 = sum((fre-2000).^2/2000 )   %116.5020
%in the hist with 100 bins expected number of
%numbers per cell is 2000
pval = 1 - chi2cdf(chi2, 100-1)    %0.1105
% The numbers are consistent with the uniform distribution
% on (0, 99999)


%OR by MATLAB's  chi2gof and k=2*n^(2/5) classes:



n = length(xx);
%k = floor( 2 * n^(2/5));
k=100;
%%
edges = linspace(min(xx),max(xx),k+1);
expectedCounts = diff(edges)*n/100000;
[h,p,st] = chi2gof(xx,'edges',edges,...                
'expected',expectedCounts)

% h = 0
% p = 0.1090
% st =  chi2stat: 116.6274
%           df: 99
%        edges: [1x101 double]
%            O: [1x100 double]
%            E: [1x100 double]



