フィルターのクリア

How do I generate numbers from an Exponential Distribution within Min and Max values?

4 ビュー (過去 30 日間)
How to generate random numbers with the exponential distribution applied, and also given a minimum value of 0.5, and a maximum value of 4.3, the mean is 1.5?
How do I add the maximum to the following?
pd = makedist('Exponential','mu', 1.5);
r=0;
while r<0.5
r=random(pd);
end

採用された回答

Star Strider
Star Strider 2023 年 7 月 11 日
Use the truncate function —
pd = makedist('Exponential','mu', 1.5);
t = truncate(pd, 0.5, 4.3);
r = random(t,10000,1);
figure
histogram(r,100)
.
  2 件のコメント
John D'Errico
John D'Errico 2023 年 7 月 11 日
編集済み: John D'Errico 2023 年 7 月 11 日
+1. Truncate is the correct solution, of course. It will sometimes be vastly more efficient compared to rejection schemes too, since random does not need to throw away a significant fraction of the generated samples.

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

その他の回答 (2 件)

Vishnu
Vishnu 2023 年 7 月 11 日
Hi Sean Sarran,
Simply insert a "or" statement in the while loop condition itself to apply a maximum value of 4.3.
Consequently, this is the whole code:
pd = makedist('Exponential', 'mu', 1.5);
r = 0;
while r < 0.5 || r > 4.3
r = random(pd);
end

Torsten
Torsten 2023 年 7 月 11 日
pd = makedist('Exponential','mu', 1.5);
n = 100;
lb = 0.5;
ub = 4.3;
rv = zeros(n,1);
i = 0;
while i < n
r = random(pd);
if r >= lb & r <= ub
i = i+1;
rv(i) = r;
end
end
rv
rv = 100×1
3.7779 1.5435 0.8186 2.5542 3.0388 2.1058 1.9611 1.3722 0.9059 3.2783

Community Treasure Hunt

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

Start Hunting!

Translated by