How to generate just one random number in every iteration of for loop?
Show older comments
I want to generate n random number in a For loop from 1:n. But, every time as the loop iteration increase the number of generated points would increase as well. I need to generate just one pair(x,y) random number in every iterations.
For i=1:10;
%generate a random x,y
x(i)=rand(1)
y(i)=rand(1)
if x(i)<0.01 && y(i)<0.5
%generate another random x,y and make comparison again
x(i)=rand(1)
y(i)=rand(1)
return
end
end
%For example when i=4 this code generate 4 random x,y which is not my favorite i want just another single x,y
Accepted Answer
More Answers (1)
dpb
on 27 Jul 2014
Not as long as you don't write the code that way it won't...
doc rand
3 Comments
Image Analyst
on 27 Jul 2014
and you're generating at least 2 random numbers, and possibly 4 (if you get into the if block). To generate just one random number on each iteration, do
for k = 1 : 10
x(k) = rand(1);
end
Image Analyst
on 27 Jul 2014
Did you see my answer?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!