I would like to generate a series of 30 random numbers where the upper limit is 40, the lower limit is 12 and the mean is 23. Summarizing:
n = 30
Min_value = 12
Max_value = 40
Mean = 23
Thanks in advance.
I would like to generate a series of 30 random numbers where the upper limit is 40, the lower limit is 12 and the mean is 23. Summarizing:
n = 30
Min_value = 12
Max_value = 40
Mean = 23
Thanks in advance.
If you want a continuous distribution, where you can get any floating point value between the min and max, one possibility is the triangular distribution which has its mean equal to (min+max+mode)/3
. Given the mean, min, and max, that would imply you need mode = 3 * mean - min - max
. That yields mode = 17
for your problem. Numpy provides a generator for triangular distributions.
If you want a discrete distribution, where the outcomes are all integers, you could consider a binomial distribution with shifting. The binomial distribution has parameters n
and p
, with the range being {0,...,n}
and the mean equal to n*p
. Since you want a min of 12 and a max of 40, you need to shift all of your target values by subtracting the min. You would generate a binomial with n = max - min = 28
and a mean of 23 - 12 = 11
. That would require 28 * p = 11
, or p = 11/28
. Generate your final outcomes by adding the min value to shift the results back to your desired range, i.e., X = binomial(n = 28, p = 11/28) + 12
.
You said in a comment that you want to do this in python, so here's some code illustrating both distributions:
import numpy as np
rng = np.random.default_rng()
sample_size = 30
min, max = 12, 40
# Triangular
mode = 17
print(rng.triangular(min, mode, max, sample_size))
# Binomial
n, p = 28, 11/28 # number of trials, probability of each trial
print(rng.binomial(n, p, sample_size) + 12)
Note that the range limits are guaranteed but values near the extremes of the range are extremely rare for both of these. If you want more occurrences of the min and max, you'll need to dig up a bounded distribution with "fatter" tails. One way to do this would be to generate a mixture of distributions, such as flipping a weighted coin to generate from either a triangular or a uniform distribution. Since the mean of the uniform is the mid-range (26), this would require you to adjust the mean of the binomial or triangle to 20 in order to yield the overall target mean of 23 if you use a 50/50 weighting. For example, to get a mix of binomials and discrete uniforms:
sample_size = 30
min, max = 12, 40
n, p = 28, 8/28 # adjust p to get mean of 20 after shifting by 12
binom = rng.binomial(n, p, sample_size // 2) + 12
unif = rng.integers(min, high = max + 1, size = sample_size // 2)
pooled = np.concatenate((binom, unif))
rng.shuffle(pooled)
print(pooled)