Randomly generated numbers do not change themselves with each iteration
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
Or maybe it only seemed that your values were not different, because you did not display with enough digits?
2 Comments
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!