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

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

I think you want
if x(i) < 0.01 && y(i) < 0.5
instead of
if x<0.01 && y<0.5

4 Comments

yes you'r right. But, it's not my answer.
Why not? Each iteration will generate one pair. Some percentage of the time the "if" statement will be true and the pair will be replaced by a different pair, but at the end, you still have one pair (an x and a y) for each iteration. As far as I can tell that meets your needs of "I need to generate just one pair(x,y) random number in every iterations." If it doesn't, say WHY it doesn't rather than just say "it's not my answer."
I see you took my advice and edited your code to include my code. The only thing I fixed was converting For to lower case and adding a printout of x and y, and correcting the indentation, and I got this code:
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
x
y
whos x
whos y
You'll see this:
Name Size Bytes Class Attributes
x 1x10 80 double
Name Size Bytes Class Attributes
y 1x10 80 double
so you can see that this proves x and y are 10 elements long, and 10 is the number of iterations. So please explain why my fix is not working.
If perhaps I read between the lines maybe I finally begin to get the underlying question -- if you write (no loop, no nothing already in existence)
k=2;
x(k)=rand(1);
the resulting length(x) will be 2 because Matlab doesn't have variable lower indices on arrays - it will fill in x(1)=0 in the above. There aren't two random values, only one of those, but the size(x) will reflect the upper bounds of the array index used.
If that is the symptom of which your complaining, don't create the index at all--if it's only one value you don't need an array at all, anyway; just use
x=rand(1);
overwriting the previous value.
Other than that I have no klew what the complaint is; try again if that isn't it.
You are right.Thank you very much...

Sign in to comment.

More Answers (1)

Not as long as you don't write the code that way it won't...
doc rand

3 Comments

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
yes,But for example if k=2 this loop generate two number. I want just one random number in each iteration...

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 27 Jul 2014

Commented:

on 27 Jul 2014

Community Treasure Hunt

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

Start Hunting!