# Generating random variables from normal distribution

19 ビュー (過去 30 日間)
Ruby 2012 年 9 月 25 日
I generated random variables from a normal distribution. I used this: normrnd(mu, sigma, N, 1). Mu = 0.146053128 and sigma = 0.13470517. The problem is some of the random numbers generated are negative.
My question is: 1. How do I generate all positive (non-negative) random numbers from normal distribution? 2. Can someone kindly explain why the negative numbers were generated since I have no idea and cannot explain why that happened.
Ruby

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

### 採用された回答

Matt Fig 2012 年 9 月 25 日

A normal distribution is defined that way. A normal distribution has numbers on either side of the mean, and they may be far to either side. Look:
Mu = 0.146053128;
sigma = 0.13470517;
N = normrnd(Mu, sigma, 10000, 1);
hist(N,-1:.05:1) % Notice the symmetry around point Mu.
So if you want all non-negative numbers, do this:
N = N + abs(min(N)); % Move everything to the right.
You could also add any number M where M> abs(min(N)).

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

### その他の回答 (2 件)

Matthew 2012 年 9 月 25 日
If you just want to truncate the tail so that you don't get negative numbers, the below would work.
normArray = normrnd(mu, sigma, N, 1);
normArray(normArray<0) = 0;
##### 3 件のコメント表示 2 件の古いコメント非表示 2 件の古いコメント
Image Analyst 2012 年 10 月 8 日
Matthew's is not normal, but Matt's isn't exactly what was asked for either. It's still normal but the mean shifted upwards from the desired location. For another way to do it (again, not producing what was wanted, or normal), you can simply remove the negative numbers altogether:
normArray(normArray < 0) = []; % Eliminate negative numbers.
This will also reduce the number of elements in the array of course, so you might want to ask for way more than is necessary and then truncate to the number you want:
normArray(desiredNumberOfElements+1:end) = []; % Get rid of extra unneeded #'s.
or alternatively:
normArray = normArray(1:desiredNumberOfElements); % Get rid of extra unneeded #'s.

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

Ruby 2012 年 9 月 26 日
Hi Matt, Thank you very much for the quick response and for the solution to my problem. You can't imagine how relieved I am right now. I have been able to generate the random variables as I wanted and also I have a clear understanding why those negative numbers occured. Once again, thank you.
Ruby
##### 1 件のコメント表示 なし非表示 なし
Matt Fig 2012 年 9 月 26 日
You're welcome!

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

### カテゴリ

Find more on Random Number Generation in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by