Normal distribution for a given range of numbers.

129 次查看(过去 30 天)
I have a range in which my parameter can vary for example, 0.38 to 0.5. I need to produce in between points such that the data will be normally distributed. I have thought of the followig way.
% code
xmin=0.38
xmax=0.5
n=20
x=xmin+randn(1,n)*(xmax-xmin);
But randn(1,n) gives me random numbers from more than 1 as well. Which does not serve the purpose of putting the range. Then my xmax becomes more than 0.5. How to achieve this for normal distribution and the given range? Thank you :)

采纳的回答

Birdman
Birdman 2018-4-16

Use rand, instead of randn:

 x=xmin+rand(1,n)*(xmax-xmin);
  7 个评论
Mostafa Nakhaei
Mostafa Nakhaei 2020-2-20
If you draw you will see that x=xmin+rand(1,n)*(xmax-xmin) is far from being normal.
try this:
p = 6;
xmin=0.38;
xmax=0.5;
n=10000;
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
hist(X,50)
you will see this is closer to normal distributation as exlained by John in the next comment.

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2018-4-17

A normal distribution does not have limits. In theory it is possible to see generated points that lie all the way out to infinity, or at least arbitrarily close to that point.

You might consider a truncated normal distribution. I know there is at least one such utility to be found on the MATLAB Central File Exchange. You can do the search as easily as can I. A truncated normal distribution is not that difficult to sample from either. The stats toolbox would make it fairly easy.

Just as easy is to make use of the central limit theorem. If you want a fairly normal looking distribution of points, that all lie within limits of xmax and xmin, do this:

p = 6;
xmin=0.38;
xmax=0.5;
n=10000;
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
hist(X,50)

Don't go overboard and make p too large however. Large values of p will see you generate very few points near those limits. As well, large values of p will take more memory and time to generate the sample.

If p is smaller, 3 for example, the distribution will look a bit less smooth, but you will more likely get points near the endpoints.

p = 3;

If you have the stats toolbox, then betarnd will work too. So we could generete samples with a distribution that looks fairly like a truncated Normal.

A = 3;
B = 3;
X = betarnd(A,B,1,n);
X = xmin + (xmax - xmin)*betarnd(A,B,1,n);
hist(X,50)

As you make A and B larger, the distribution will start looking vaguely more normally distributed, but the number of events that occur near your limits will start to drop again.

So it all depends on exactly how you want that distribution to behave. But as I have shown, there are lots of ways to do this.

  7 个评论
Aman Kumar
Aman Kumar 2022-2-16
X = xmin + (xmax - xmin)*sum(rand(n,p),2)/p;
Please Define the all variable used in this line

请先登录,再进行评论。


siranjeevi gurumani
编辑:siranjeevi gurumani 2022-6-28
Use rescaling to get a random normal distribution between the desired range if mean and standard deviation is not a concern
xmin=0.38;
xmax=0.5;
n=2000;
temp = normrnd(0,1,1,n);%generating a standard normal distribution
x=xmin+(xmax-xmin)*((temp-min(temp))/(max(temp)-min(temp)));%rescaling the range
hist(x)

Community Treasure Hunt

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

Start Hunting!

Translated by