Randomly generated numbers do not change themselves with each iteration

1 view (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
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!