For loop that keeps only the first result of a random number generator function for all the loop runs
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Cris LaPierre
2020년 12월 28일
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
추가 답변 (2개)
Adam Danz
2020년 12월 28일
편집: Adam Danz
2020년 12월 28일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!