Generate 2 random numbers x and y for 10 times in a loop. x can take any value in the range of (0,1) but y is conditioned on x such that y assumes any number between (0,1-x)
Show older comments
for i=1:10
random_x=rand
random_y=
Accepted Answer
More Answers (2)
RANGA BHARATH
on 21 Jun 2023
Edited: RANGA BHARATH
on 21 Jun 2023
Question: How to use the rand() function when the range parameters are conditioned on any other variable?
Solution:
You can simply define the independent variable first and then use it in defining the dependent variable.
To be more specific, once you define the x, you can use y = rand()*(1 - x).
Code:
x = zeros(1,10);
y = zeros(1,10);
for i=1:10
temp = rand(1);
x(1,i) = temp;
y(1,i) = rand(1)*(1-temp);
end
x
y
Aakash
on 21 Jun 2023
0 votes
You can use this:
for i = 1:10
x = rand();
y = rand()*(1-x);
end
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!