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)
3 ビュー (過去 30 日間)
古いコメントを表示
for i=1:10
random_x=rand
random_y=
0 件のコメント
採用された回答
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)
0 件のコメント
その他の回答 (2 件)
RANGA BHARATH
2023 年 6 月 21 日
編集済み: RANGA BHARATH
2023 年 6 月 21 日
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!