# random numbers between a and b with specified mean and variance

55 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

James Tursa
on 2 Sep 2015

1) a + b*rand() creates a value between a and a+b, not between a and b.

2) The values will have a uniform distribution with mean and variance fixed depending on a and b. If you want other mean and variance values then you either need a different a and b or you need a different distribution. Please specify what exactly you want.

##### 2 Comments

James Tursa
on 2 Sep 2015

Edited: James Tursa
on 2 Sep 2015

### More Answers (2)

Walter Roberson
on 2 Sep 2015

If you require your distribution to be probability zero outside of [a,b], the way that uniform random has all of its probability within a pre-defined range, then you need to be using a Beta distribution. Uniform random distribution is one case of the Beta distribution.

However, the mean and standard deviation of a beta distribution cannot be chosen arbitrarily: they are fixed by the choice of a and b. http://www.mathworks.com/help/stats/betastat.html

There is no other distribution that is 0 probability outside of the given interval. If [a, b] are intended to be the complete bounds with no chance of a random number outside [a,b] then you need a Beta (or the special Beta that is Uniform random).

You can truncate an infinite random distribution to [a,b], but if you generate from the result, the samples you generate will not have mean c or standard deviation d. For example,

function x = truncrandn(a,b,c,d,m,n)

x = c + d * randn(m,n);

x = min(b,max(a,x));

end

This will produce random numbers in the specified range but exactly a and exactly b will both be over-represented, and the distribution of the result will probably not have the correct mean and will definitely not have the correct standard deviation.

##### 1 Comment

Greig
on 3 Sep 2015

Just to clarify your comments on the Beta distribution....

The standard Beta distribution is bound to the range [0,1], not [a,b]. a and b control the shape of the distribution, but regardless of their choice (both must be > 0), the distribution is alway bound to [0,1].

There is a generalized Beta distribution, B(a, b, c, d), which is bound to [c,d]. This is not currently supported by MATLAB, but the rescaling is a simple linear transformation, so it should be easy to do.

Nevertheless, the mean and variance of B(a,b,c,d) depend on a,b,c, and d. So, as you correctly point out for the standard Beta distribution, they are not independent and cannot be arbitrarily defined.

Greig
on 3 Sep 2015

As far as I aware there is no bounded probability distribution where the mean and variance are independent of each other, and independent of the distribution parameterization. I don't believe it is mathematically possible. The bounds impose constraints the variance and, if the distribution is not symmetric about the mean, they also influence the mean.

Perhaps if you give us some more information about your application, we might be able to help with some approximation that fits your use. That is, something similar to James Tursa's answer and comment above.

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!