Randomly generated numbers do not change themselves with each iteration

2 views (last 30 days)
Hi everyone,
I have a long code and at the end of it I reach a maximum values using tons of randomly generated numbers and distribution method. Now, I want to put this whole code into for loop and for each iteration, since those random number generation functions will generate different numbers in each iterations, I want to obtain different maximum values. I simply added a "for l=1:4" at the beginning and at the end, I added Maxx(l)=max(Difference).There, "difference" is (1000,1) vector for 1000 randomly generated numbers. Instead of obtaining 4 different max result caused from different random number generation, I got same result every iteration. What might be the reason and how can I solve this? Thank you for your help.

Answers (1)

the cyclist
the cyclist on 8 Jun 2022
Can you post your code? This miniature version of what you described seems to work.
rng default
format long
for l = 1:4
difference = rand(1000,1);
maxx(l) = max(difference);
end
maxx
maxx = 1×4
0.999491620097705 0.999477858635892 0.999792564444752 0.999773122694756
Or maybe it only seemed that your values were not different, because you did not display with enough digits?
  2 Comments
Nevzat Can Yerlikaya
Nevzat Can Yerlikaya on 9 Jun 2022
my codes is a bit complicated. If you have any questions I would be more than pleased to answer them. I have created different random number generations with different pdf's but I believe that nooone from there change its value in each iteration shows that problem is somewhere else. I put my code below:
Maxx=zeros(1,4);
Difference=zeros(1000,1);
A=[0.011111111 0.522222222 0.244444444 0.816666667 0.833333333; 0.977777778 0.6 0.705555556 0.827777778 0.411111111; 0.666666667 0.711111111 0.322222222 0.794444444 0.455555556; 0.033333333 0.944444444 0.633333333 0.961111111 0.544444444; 0.033333333 0.972222222 0.8 0.305555556 0.983333333; 0.022222222 0.705555556 0.116666667 0.85 0.522222222; 0.1 0.638888889 0.422222222 0.827777778 0.622222222; 0.027777778 0.85 0.172222222 0.783333333 0.766666667; 0.022222222 0.694444444 0.15 0.811111111 0.994444444; 0.027777778 0.511111111 0.655555556 0.744444444 0.616666667; 0.016666667 0.405555556 0.933333333 0.805555556 0.127777778; 0.033333333 0.961111111 0.416666667 0.85 0.866666667; 0.011111111 0.616666667 0.027777778 0.822222222 0.377777778; 0.711111111 0.522222222 0.211111111 0.861111111 0.427777778; 0.794444444 0.366666667 0.133333333 0.916666667 0.494444444; 0 0.638888889 0.1 0.877777778 0.772222222; 0.827777778 0.661111111 0.533333333 0.855555556 0.272222222; 0.994444444 0.333333333 0.994444444 0.816666667 0.511111111];
%order=unforeseen soil conditions-1, delay of materials-2, adverse weather
%conditions-3, labor productivity-4, equipment efficiency & workability-5
AvgRiskFactor=[0.79, 0.77, 0.79, 0.89, 0.7];
AvgSchedule=[19.05, 18.1, 17.62, 21.9, 19.4];
Dur=[28 154 98 84 56 14 112 28 28 28 28 28 28 28 56 28 56 14];
Pred=[0 0 0; 1 0 0; 1 0 0; 3 0 0; 2 0 0; 4 0 0; 3 0 0; 4 0 0; 5 7 0; 8 0 0; 9 0 0; 10 0 0; 11 0 0; 12 0 0; 9 0 0; 11 0 0; 10 0 0; 13 15 16];
Ex=[1.61397/11.7942 1.10859/10.5958 1.20380/11.8560 1.20380/11.8560 1.16531/11.7584 1.35478/6.22609 1.40758/6.26373 0.21853/5.27009 0.67251/3.47943 0.29921/5.25454 0.47619/4.46567 1.10859/10.5958 1.19551/5.79893 1.16531/11.7584 0.51161/4.63748 0.51161/4.63748 0.53776/5.12427 1.19551/5.79893];
StdDev=Dur.*Ex;
for i=1:18
Atop(i)=A(i,1)+A(i,2)+A(i,3)+A(i,4)+A(i,5);
for j=1:5
AvgMatrix(i,j)=A(i,j)/Atop(i);
end
end
%Changing matrix so addition of probability of 5 risk factor would give 1
for l=1:4
rng('default');
x = 0:.001:1;
pretruncMean = AvgRiskFactor(5);
pretruncMean_sch = AvgSchedule(5);
%for triangular for first 4, truncated for 5th
for i=1:18
for j=1:5
if j==5
pretruncSD(i) = StdDev(i)/100;
untruncated(i) = makedist('Normal',pretruncMean,pretruncSD(i));
truncated(i) = truncate(untruncated(i),0,1);
r_prob{i,j} = random(truncated(i),1000,1);
else
pd_prob(i,j)=makedist('Triangular','A',0,'B',AvgRiskFactor(j),'C',1);
r_prob{i,j}=random(pd_prob(i,j),1000,1);
end
end
end
for i=1:18
for j=1:5
if j==5
pretruncSD_sch(i) = StdDev(i);
untruncated_sch(i) = makedist('Normal',pretruncMean_sch,pretruncSD_sch(i));
truncated_sch(i) = truncate(untruncated_sch(i),10,30);
r_sch{i,j} = random(truncated_sch(i),1000,1);
else
pd_sch(i,j)=makedist('Triangular','A',10,'B',AvgSchedule(j),'C',30);
r_sch{i,j}=random(pd_sch(i,j),1000,1);
end
end
end
%for triangular for first 4, truncated for 5th
%for random activity%
r_logn=zeros(18,1000);
r_loglog=zeros(18,1000);
r_logi=zeros(18,1000);
r_weib=zeros(18,1000);
r_norm=zeros(18,1000);
mu=0;
for i=1:18
if Type(i)==0
m=Dur(i);
v=StdDev(i)*StdDev(i);
mu=log((m^2)/sqrt(v+m^2));
sigma=sqrt(log(v/(m^2)+1));
rng('default')
r_logn(i,:)=lognrnd(mu,sigma,[1,1000]);
elseif Type(i)==1
m=Dur(i);
v=StdDev(i);
mu=log((m^2)/sqrt(v+m^2));
sigma=sqrt(log(v/(m^2)+1));
pd_loglog=makedist("Loglogistic", "mu", mu, "sigma", sigma);
r_loglog(i,:)=random(pd_loglog,1,1000);
elseif Type(i)==2
mu=Dur(i);
sigma=StdDev(i);
pd_logi=makedist("Logistic", "mu", mu, "sigma", sigma);
r_logi(i,:)=random(pd_logi,1,1000);
elseif Type(i)==3
mu=Dur(i);
sigma=StdDev(i);
pd_norm=makedist("Normal", "mu", mu, "sigma", sigma);
r_norm(i,:)=random(pd_norm,1,1000);
W=wblfit(r_norm(i,:));
pd_weib=makedist("Weibull","a",W(1),"b",W(2));
r_weib(i,:)=random(pd_weib,1,1000);
end
end
r=r_logn+r_loglog+r_logi+r_weib;
%for random activity%
%after distribution is complete
for i=1:18
for j=1:5
P{i,j}=AvgMatrix(i,j)*r_prob{i,j};
D{i,j}=P{i,j}.*r_sch{i,j};
%D{i,j}=round(C{i,j});
end
Finalcell{i,1}=D{i,1}+D{i,2}+D{i,3}+D{i,4}+D{i,5};
Finalarray(i,:)=Finalcell{i,1};
end
for i=1:18
for k=1:1000
if Pred(i,1)==0 && Pred(i,2)==0 && Pred(i,3)==0
Str_final(k,i)=0;
Fin_final(k,i)=Str_final(k,i)+r(i,k)+Finalarray(i,k);
else
for j=1:3
Activ_final(i,j)=Pred(i,j);
if Activ_final(i,j)~=0
Ende_final(i,j,k)=Fin_final(k,Activ_final(i,j));
else
Ende_final(i,j,k)=0;
end
end
Str_final(k,i)=max(Ende_final(i,:,k))+1;
Fin_final(k,i)=Str_final(k,i)+r(i,k)+Finalarray(i,k);
end
end
end
Difference=(Fin_final(:,18)-Fin(:,18));
Maxx(l)=max(Difference);
end
subplot(2,1,1);
histogram(Maxx,10);
the cyclist
the cyclist on 9 Jun 2022
You set
rng('default');
at the beginning of each loop. That means that each loop starts from the same random number generator seed. You should pull that line outside of the loop, as it is in my example.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!