Use rand, instead of randn:
x=xmin+rand(1,n)*(xmax-xmin);
Use rand, instead of randn:
x=xmin+rand(1,n)*(xmax-xmin);
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.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!