Borrar filtros
Borrar filtros

How to generate just one random number in every iteration of for loop?

4 visualizaciones (últimos 30 días)
I need to generate just one random number in every iterations.
For i=1:10
x(i)=rand(1)
end
%For example when i=4 (x(4)), this code generate 4 random x which is not my favorite i want just another random x

Respuesta aceptada

Robert Cumming
Robert Cumming el 27 de Jul. de 2014
Your storing each random created in each loop, if you only want one just do
x = rand (1);
  2 comentarios
hamed
hamed el 27 de Jul. de 2014
Editada: hamed el 27 de Jul. de 2014
I need to store them because these are the initial positions of a serie of particles. It is a part of code.
Robert Cumming
Robert Cumming el 27 de Jul. de 2014
That's what you have in your original code.
At i=4 its generating the 4th random number and saving it in x(4). X(1:3) are already populated by the earlier loops.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by