フィルターのクリア

Random number between 0 and the value of another variable

2 ビュー (過去 30 日間)
SY
SY 2017 年 12 月 31 日
コメント済み: SY 2017 年 12 月 31 日
Hello, Is there an easy way for me to set the value of a one variable as a restriction when generating a random number? Specifically, what I want to do is:
1. "p" is generated as a random number between 0 and 1
2. Generate another variable "q", but there is a restriction that p+q < 1. Since I determined the value of p in the previous step, I'd like q to be something like rand(1-p), but obviously the rand function requires that size inputs must be integers.
Is there a clever way to do this? What I've thought of is making an if statement to check that p + q < 1 before proceeding to the next steps of the code (and repeat the rand function if q happens to be too large), but I thought there may be a simpler solution.
Thanks!
SY

採用された回答

James Tursa
James Tursa 2017 年 12 月 31 日
q = rand*(1-p);
  1 件のコメント
SY
SY 2017 年 12 月 31 日
My goodness, it was this simple! Can't believe all I had forgotten was the * sign this entire time. Thanks very much!
SY

サインインしてコメントする。

その他の回答 (1 件)

ANKUR KUMAR
ANKUR KUMAR 2017 年 12 月 31 日
As per my knowledge, there is no any predefined function which do your work. You can write the script as well or you can write function for your convenience.
clc
clear
A=rand(5)
[x y]=find(A>0.5);
while length(x)>0
for i=1:length(x)
A(x(i),y(i))=rand(1);
end
[x y]=find(A>0.5);
end
A
  1 件のコメント
SY
SY 2017 年 12 月 31 日
Thanks for the help Ankur!
SY

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by