Finding random normally distributed number above zero

4 ビュー (過去 30 日間)
Tishan Anantharajah
Tishan Anantharajah 2023 年 4 月 15 日
コメント済み: Paul 2023 年 4 月 15 日
I am currently using randn to produce a normally distributed number however would like to only take the positive side for the normally distributed curve. How could this be done? Would I have to use a loop?

回答 (3 件)

Torsten
Torsten 2023 年 4 月 15 日
編集済み: Torsten 2023 年 4 月 15 日
Maybe taking abs(randn()) will give you something useful. It's the folded normal distribution (which in this case equals the truncated normal distribution):
n = 100000;
y = abs(randn(n,1));
hold on
histogram(y,'Normalization','pdf')
fun = @(x)sqrt(2/pi)*exp(-x.^2/2);
x = 0:0.01:4;
plot(x,fun(x))
hold off
  2 件のコメント
Tishan Anantharajah
Tishan Anantharajah 2023 年 4 月 15 日
Thank you it works
Paul
Paul 2023 年 4 月 15 日
Can also use the HalfNormalDistribution if it's needed to do more than just generate random numbers.
n = 100000;
y = abs(randn(n,1));
hold on
histogram(y,'Normalization','pdf')
x = 0:0.01:4;
plot(x,pdf('HalfNormal',x,0,1))
hold off

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


Star Strider
Star Strider 2023 年 4 月 15 日
The truncate function is another option.

Image Analyst
Image Analyst 2023 年 4 月 15 日
You could just throw away any less than zero:
r = stDev * randn(100000, 1) + desiredMeanValue;
r(r<=0) = [];
% Or equivalently
r = r(r > 0);
To learn other fundamental concepts, invest 2 hours of your time here:

カテゴリ

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