# Random number generation with min, max and mean

117 views (last 30 days)

Show older comments

Nevzat Can Yerlikaya
on 25 May 2022

Commented: John D'Errico
on 25 May 2022

##### 6 Comments

Torsten
on 25 May 2022

Edited: Torsten
on 25 May 2022

### Accepted Answer

John D'Errico
on 25 May 2022

Edited: John D'Errico
on 25 May 2022

Sigh. I've probably seen different variations of this question asked at least many hundreds of times over the 40+ years I have been answering questions and doing consulting. The problem is, it has no answer. At least not one that you will be happy with in the form you asked it. That is, there are infinitely many distributions one might choose that have the properties you describe.

For example, a beta distribution is a nice example, or perhaps a truncated normal distribution. They could both have the general curve shape you describe for the PDF. But even in those two specific cases there are infinitely many choices one could make, ALL of which satisfy the requirements. And you have not given sufficient information to choose between anything.

For example, consider a beta distribution.

The one they describe there is defined on a support of [0,1], so it is perfect for your problem. That beta distribution has a mean of

alpha/(alpha + beta)

so as long as the two distribution parameters satisfy the relationship:

0.6*alpha = 0.4*beta

then the mean will be 0.4.

Since you seem to want a unimodal distribution, then they both need to be greater than 1 too. But that does not restrict things by a lot. There are still infinitely many distributions that will do as required. Conveniently, the stats toolbox beta distribution is also defined on a support of [0,1], so that is good. Here are a few distributions that all have the necessary properties:

fplot(@(X) betapdf(X,1.6,2.4),[0,1])

hold on

fplot(@(X) betapdf(X,2,3),[0,1])

fplot(@(X) betapdf(X,4,6),[0,1])

fplot(@(X) betapdf(X,6,9),[0,1])

fplot(@(X) betapdf(X,10,15),[0,1])

grid on

legend('[1.6,2.4]','[2,3]','[4,6]','[6,9]','[10,15]')

Again, as long as alpha is exactly 2/3 of beta, then the mean will be 0.4. There would be no beta distribution where the mode AND the mean will be exactly 0.4, but as the parameters grow large in that ratio, the mode will approach 0.4 asymptotically.

I could surely do something similar for a truncated normal distribution or many others, or I could hack up an example with the mode and mean both at 0.4, though that would require me to think for a few seconds more, and it is still too early in the AM here for the brain cells to fully function.

Still, the fundamental problem is you have not said enough in your requirements. Pick any of the distributions I have shown, and they will all have a mean of 0.4, and the mode will not be too far from 0.4 either. They are all unimodal distributions.

##### 2 Comments

John D'Errico
on 25 May 2022

To generate a 1xn vector of samples with a specific choice of alpha and beta...

n = 10;

betarnd(4,6,[1,n])

If you have cases where the mean should be different than 0.4, then you can just use the formula I gave, derived from the mean of the beta distribution. Effectively, if you know the mean should be mu, then we have

alpha/(alpha + beta) = mu

so we can solve for alpha as a ratio compared to beta.

alpha/beta = mu/(1-mu)

Thus with mu = 0.2, that tells you to choose any alpha and beta such that alpha is 1/4 of beta, though again they both need to be strictly greater than 1.

fplot(@(X) betapdf(X,1.1,4.4),[0,1])

hold on

fplot(@(X) betapdf(X,1.5,6),[0,1])

fplot(@(X) betapdf(X,3,12),[0,1])

fplot(@(X) betapdf(X,10,40),[0,1])

Again, all of the plotted distributions have a mean of 0.2, and I cannot rationally choose between them.

Based on your last comment, you might choose a simple uniform distribution on some interval. For example, if the mean should be 0.2, you might decide to choose a uniform distribution on the interval [0.1,0.3]. (So then you could use unifrnd, or even rand would suffice with a transformation.) The problem is again, if you choose a uniform distribution with a mean, for example, of 0.2, then how wide a range will you allow? So a uniform distribution on the interval [0.1,0.3] and one on the interval [0.15,0.25] both have means of 0.2. But they are very different in the results you will see. Your problem is still that of non-uniqueness.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!