For loop that keeps only the first result of a random number generator function for all the loop runs
2 views (last 30 days)
Show older comments
Hi,I have this for loop
function[result]=temperature_grid(f,X,Y)
for i=1:length(Y)
for j=1:length(X)
result(i,j) = f(X(j),Y(i))
end
end
where the f is this random generator function which I call function[re]=tmprtr(X,Y)
Kx=randi([1,5]);
Lx=randi([1,5]);
Ky=randi([1,3]);
Ly=randi([1,3]);
re=111 .*e.^(-0.3.*(((X-Lx).^2)+0.6 .*((Y-Ly).^2)))+66 .*e.^(-0.2 .*(((X-Lx).^2)+1.6 .*((Y-Ly).^2)));
I want it to produce only once random numbers and use these same numbers for all the other runs of the loop but now with every run of the loop it creates new random numbers which is something I don't want...Do you have any ideas how to make it work?
0 Comments
Accepted Answer
Cris LaPierre
on 28 Dec 2020
You would have to generate the random numbers before entering the loop, or inside an if statement inside the loop that only executes if the loop counter is 1.
0 Comments
More Answers (2)
Adam Danz
on 28 Dec 2020
Edited: Adam Danz
on 28 Dec 2020
Select a random seed for the generator and reset the generator every time you want to repeat the same results.
Seeds can be any positive integer up to 2^32-1 so you can select a seed using
randSeed = randi(2^32-1);
rng(randSeed,'twister')
or
rng('default')
rng(randSeed)
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!