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)

조회 수: 2 (최근 30일)
for i=1:10
random_x=rand
random_y=

채택된 답변

Samay Sagar
Samay Sagar 2023년 6월 21일
You can use the following to generate random numbers between 0 and 1-x
y=rand()*(1-x)
%To generate random no.s between a and b : y = a + rand()*(b-a)

추가 답변 (2개)

RANGA BHARATH
RANGA BHARATH 2023년 6월 21일
편집: RANGA BHARATH 2023년 6월 21일
Hi @Payel. Here is the solution and code for your question.
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
x = 1×10
0.6590 0.7347 0.9144 0.6091 0.3819 0.4112 0.1823 0.8654 0.5492 0.6796
y
y = 1×10
0.3405 0.1315 0.0718 0.1244 0.3935 0.3454 0.7109 0.1107 0.1352 0.1733

Aakash
Aakash 2023년 6월 21일
You can use this:
for i = 1:10
x = rand();
y = rand()*(1-x);
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by