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

조회 수: 3 (최근 30일)
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

채택된 답변

Robert Cumming
Robert Cumming 2014년 7월 27일
Your storing each random created in each loop, if you only want one just do
x = rand (1);
  댓글 수: 2
hamed
hamed 2014년 7월 27일
편집: hamed 2014년 7월 27일
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 2014년 7월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by