Last active
September 28, 2017 11:32
-
-
Save tcibinan/1ac9e0b5d4d4c6e685f9d18d0ab47248 to your computer and use it in GitHub Desktop.
Generate random values from 0 to 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generator() | |
m = 2^31 - 1; | |
a = 630360016; | |
n = 1001; | |
count = 1000; | |
y = zeros(count, 1); | |
ec = zeros(count, 1); | |
ec(1) = 34238443; | |
for i = 2:1:n | |
ec(i) = mod(a*ec(i-1),m); | |
y(i) = ec(i)/m; | |
end | |
% Expected value | |
mat = 0; | |
for i = 1:1:n | |
mat = mat + y(i); | |
end | |
mat = mat/count; | |
fprintf('Expected value: %f\n', mat) | |
% Dispersion | |
dis = 0; | |
for i = 1:1:n | |
dis = dis + (y(i)-mat)^2; | |
end | |
dis = dis/count; | |
fprintf('Dispersion: %f\n', dis) | |
% Correlation | |
kSize = 20; | |
k = zeros(kSize,1); | |
for i = 1:1:kSize | |
for j = 1:1:(count-i-1) | |
k(i) = k(i) + (y(j)-mat)*(y(j+i-1)-mat); | |
end | |
k(i) = k(i)/(count-i-1); | |
end | |
subplot(2,2,3); | |
plot(0:1:kSize-1,k); | |
title('Correlation'); | |
% (y(i+1), y(i)) | |
X = zeros(count, 1); | |
Y = zeros(count, 1); | |
for n = 1:count | |
X(n) = y(n); | |
Y(n) = y(n+1); | |
end | |
subplot(2,2,2); | |
scatter(X, Y); | |
title('(y(i+1), y(i))'); | |
% Histogram | |
ints = 60; | |
step = 1/ints; | |
borders = 0:step:1; | |
hist_arr = zeros(ints+1, 1); | |
i = 0; | |
for b = 0:step:1 | |
q = b + step; | |
i = i + 1; | |
for j = 1:count | |
if b<y(j) && y(j)<q | |
hist_arr(i) = hist_arr(i) + 1; | |
end | |
end | |
hist_arr(i) = hist_arr(i)/1000/step; | |
end | |
subplot(2,2,1); | |
bar(borders, hist_arr); | |
title('histogram'); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment