# Random number generation with min, max and mean

23 views (last 30 days)
Nevzat Can Yerlikaya on 25 May 2022
Commented: John D'Errico on 25 May 2022
Hello, I want to generate random numbers for probability function with using mean, maximum and minimum values. For example, I know the average probability of X is 0,4. What I want is to reate random numbers between 0 and 1 with most of them close to 0,4. Is there a function like this? Does this contradict the randomness of random numbers? Thank you for your help!
Torsten on 25 May 2022
To account for the effect of a risk event on the schedule, you must know the general probability that a risk event happens at time t, not the probability of what kind of risk event happened under the condition that it has happened. So to know that 10% of the risk events are due to fire and 85% are due to worker accidents does not help much in your case.

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 CommentsShowHide 1 older comment
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])
ans = 1×10
0.6843 0.4984 0.2694 0.4998 0.1733 0.4502 0.3860 0.4917 0.4207 0.4356
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.

R2021b

### Community Treasure Hunt

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

Start Hunting!