For loop that keeps only the first result of a random number generator function for all the loop runs
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Bulldog
el 28 de Dic. de 2020
Respondida: Bulldog
el 28 de Dic. de 2020
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 comentarios
Respuesta aceptada
Cris LaPierre
el 28 de Dic. de 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 comentarios
Más respuestas (2)
Adam Danz
el 28 de Dic. de 2020
Editada: Adam Danz
el 28 de Dic. de 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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!