2

I want to generate Gaussian random numbers in MATLAB for long program which runs for many number of iterations. I used randn function, but is there a way to avoid negative results and generate random numbers in range from 1 to 100.

For example

X=0.02*randn;

How Can I get only positive values in specific range.

1
  • 2
    If you want the number to be between 1 and 100, it won't be a Gaussian distribution.
    – Oli
    Jan 24, 2012 at 18:52

3 Answers 3

4

As Cheery wrote, a Gaussian distribution covers the whole real set, so there is no way to have numbers both normally distributed and limited in support.

A solution might be to truncate the values: regenerate the values when randn returns a value outside of the desired range.

This can be implemented quite easily (and naively) by the following code:

function x = randnlimit(mu, sigma, minVal, maxVal, varargin);

assert(mu>=minVal && mu<=maxVal);
assert(sigma>0);

x = mu + sigma*randn(varargin{:});
outsideRange = x<minVal | x>maxVal;
while nnz(outsideRange)>0
   x(outsideRange) = mu + sigma*randn(nnz(outsideRange),1);
   outsideRange = x<minVal | x>maxVal;
end

edit to summarize the discussion @Cheery and I had: You can choose: either you get a Gaussian, but then you are stuck with values that cover the whole real axis (so also negative values). On the other hand, if you need a limited range, you need to use a different distribution to generate samples from.

Which approach you need depends on your application. Whether the need for a limited support is primordial or the shape of the pdf is the most important.

The code I provided above will be limited to the range [minVal, maxVal] and approximately gaussian when you choose sigma and mu appropriately, i.e. mu = maxVal/2 + minVal/2 and n * sigma = maxVal - minVal. For a value of n larger than two, the distribution will be quite close to a real Gaussian. E.g. for n=2, I expect only 5% difference (for n=3, less than 1%). You can of course specify minVal = 0 and maxVal = +Inf to select the positive values only.

2
  • 1
    "A solution might be to truncate the values" - this will break the distribution!!! From programming point of view it is not a problem, but from the mathematical - a serious one.
    – Cheery
    Jan 24, 2012 at 18:55
  • You are right that it will not be Gaussian. I explicitly wrote that it is impossible to have both Gaussian and a pdf that has a limited support. This truncation method is just an effort at making random values that have a bell-shaped pdf over the desired range.
    – Egon
    Jan 24, 2012 at 19:12
0

A Gaussian distribution by definition has a distribution of (-inf, inf). The standard deviation (sigma) is 1 by default. If you're looking for a uniform distribution over [1, 100] use 99*rand()+1 or randi([1 100]) for integers.

ps: for Gaussian distribution with range there is a solution (by setting the sigma and shifting maximum of the distribution) http://www.mathworks.com/matlabcentral/newsreader/view_thread/156521

function X=random_generator(n, x_max, x_min)
    X=[x_min+((randn(n,n)).*(x_max-x_min))];
end

x_min here is not the minimum value - it is the average value (or the peak of the distribution). Distribution is symmetrical with respect to x_min. But negative values would not be removed as distribution defined on the whole X axis. Their probability will be smaller.

pps: from Matlab's manual

Generate values from a normal distribution with mean 1 and standard deviation 2: 1 + 2.*randn(100,1);

7
  • 1
    This code will NOT generate normally distributed samples. It will generate uniformly distributed samples over a scaled interval. No distribution with limited support can be a real Gaussian, but that code you cited from your source doesn't even come close.
    – Egon
    Jan 24, 2012 at 18:56
  • @Egon Geerardyn why not? sigma is set to (x_max-x_min) and distribution is shifted by x_min (which is mu or average value in the normal distribution). FYI! (from the Matlab manual) Generate values from a normal distribution with mean 1 and standard deviation 2: r = 1 + 2.*randn(100,1);
    – Cheery
    Jan 24, 2012 at 18:58
  • 1
    Because if x ~ Uniform(a,b) (read: x is uniformly distributed over [a,b]), then cx ~ Uniform(ac, a*b) and c + x ~ Uniform(a+c,b+c) for all constants a,b and c. The function rand returns uniformly distributed samples. You can verify this for yourself by generating lots of samples and plotting its histogram. This will be quite a constant empirical pdf, certainly not bell-shaped.
    – Egon
    Jan 24, 2012 at 19:07
  • @Egon Geerardyn I cited the Matlab manual with example. mathworks.com/help/techdoc/ref/randn.html
    – Cheery
    Jan 24, 2012 at 19:09
  • 1
    The function you copied from your reference mentions rand, not randn. With randn, it indeed produces samples from a normal distribution with a clumsily specified range (as stated in the manual). But that will NOT prevent from generating negative samples. It is impossible to have both an exact Gaussian and limited support.
    – Egon
    Jan 24, 2012 at 19:17
0

You have to choose a limited support distribution that have the desired properties, check http://en.wikipedia.org/wiki/List_of_probability_distributions. I have a similar problem I want to apply finite mixtures model to a limited support distribution, unfortunately, most of the algorithms focus on Gaussian distributions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.