Skip to content

Instantly share code, notes, and snippets.

@carbolymer
Created June 16, 2012 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbolymer/2942050 to your computer and use it in GitHub Desktop.
Save carbolymer/2942050 to your computer and use it in GitHub Desktop.
clear;
function X = generate(a,c,m)
X = zeros(1000,1);
X(1) = 1;
for i = 1:999
X(i+1) = mod(a*X(i)+c,m);
endfor
endfunction
function h = histogen(X, nbins)
maxval = max(X);
h = zeros(nbins,1);
for i = 1:nbins
for j = 1:1000
if X(j) > (i-1)/nbins*maxval && X(j) < i/nbins*maxval
h(i) = h(i) + 1;
endif
endfor
endfor
endfunction
a = [69069 ; 16807 ; 1664525 ; 1103515245];
c = [1 ; 0 ; 1013904223 ; 12345];
m(1) = power(2,32);
m(2) = power(2,32) - 1;
m(3) = power(2,32);
m(4) = power(2,32);
nbins = 10;
T = ones(nbins,1).*100;
X1 = generate(a(1),c(1),m(1));
h1 = histogen(X1,nbins);
c1 = sum((T - h1).^2./T);
X2 = generate(a(2),c(2),m(2));
h2 = histogen(X2,nbins);
c2 = sum((T - h2).^2./T);
X3 = generate(a(3),c(3),m(3));
h3 = histogen(X3,nbins);
c3 = sum((T - h3).^2./T);
X4 = generate(a(4),c(4),m(4));
h4 = histogen(X4,nbins);
c4 = sum((T - h4).^2./T);
figure(1);
bar(h1);
figure(2);
bar(h2);
figure(3);
bar(h3);
figure(4);
bar(h4);
c1
c2
c3
c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment