MATLAB: For loop that keeps only the first result of a random number generator function for all the loop runs

forloopnumberrandirandom

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?

Best Answer

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.