normal distribution range

Hello, I would like to use 'randn' create random numbers normally distributed. I noticed that it creates positive and negative numbers with mean 0.
How do I create only positive numbers, say within a range 1 to 100 with a mean of around 50 (std dev can be 2).
I noticed that we can specify the mean and std dev, but not the range. How can I specify the range too.
Thanks.

2 个评论

Why don't you want negative numbers? Strictly speaking, a normal distribution with bounds on it is not a normal distribution. However, in the example you provided, a negative number should be extremely rare.
This is for a problem that that has X number of items numbered from 1-X. We choose the numbers randomly (normally distributed). Since the items are numbered positive, I wanted my range to be from 1-X.
Thanks.

请先登录,再进行评论。

回答(3 个)

I encourage you to be very careful about your terminology. It is contradictory to say that your variable has a range from 1-100, and is normally distributed (because a truly normal distribution has infinite tails).
A distribution that is truly finite in extent, but can be made to look "normal-ish", is the beta distribution. For example, take a look at this:
>> hist(100*betarnd(5,5,10000,1),50)
Finally, it is not really clear to me whether this is a discrete or a continuous distribution. You mention "number of items", which suggests a discrete count. But the distributions being suggested are continuous. Is it OK to have 64.7 items?
Please take a look at my FEX submission.
% Generate 1000000 numbers with mean ~ 50, range = [1 100], std = 20
X = 50+TruncatedGaussian(20,[1 100]-50,[1 1e6]);
>> min(X)
ans =
1.0008
>> max(X)
ans =
99.9996
>> mean(X)
ans =
50.0717
>> std(X)
ans =
19.9901
>> hist(X)
% Bruno

4 个评论

Thank you so much!!
This is just what I need.
You could best thank Bruno by accepting the answer.
Hi Bruno, I cannot use that formula because matlab does not define it. I use Matlab R2017a. Is it a problem of Matlab version
It's an add-on from the File Exchange, not a function in a MathWorks product.
You can follow the link Bruno posted to download it then install it. Alternately you can click on the "Add-Ons" button in the Toolstrip, search for "truncated gaussian", open the submission from Bruno, and add it to your installation using the Add button. In this second case, MATLAB will default to putting it on your MATLAB path.

请先登录,再进行评论。

Oleg Komarov
Oleg Komarov 2011-1-27

1 个投票

Directly from the example in the documentation of randn:

r = 50 + 2.*randn(10000,1);
hist(r,100)

Oleg

3 个评论

Hello!
Thank you for your reply.
I noticed this one ranges from 42-56.
I would like my dist to have a mean of 50, but with a range form 1-100. does this mean I have to increase my std dev to 30 or 40?
Thanks1
[ I noticed this one ranges from 42-56. ]
Wrong. range of Gaussian is always [-Inf,Inf].
Most of the time you can believe in your eye. But sometime you won't.
Bruno
I just suggest:
1) Use Bruno's function
2) Play with the example I posted, augmenting the number of draws and changing stdev etc...

请先登录,再进行评论。

提问:

2011-1-27

Community Treasure Hunt

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

Start Hunting!

Translated by