2

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.

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
turaran32
  • 83
  • 8
  • Thanks Peter for your answer in my case I don't need the total sum and it would be to do it in python. Would you know if there is any other article? (I have been looking and I have not found much) or how could it be done? – turaran32 Jul 07 '21 at 12:57
  • this question isn't well defined, you'd need to specify the distribution these values should be sampled from. also, those limits are not symmetrical around the mean, which might make this awkward. or do you just want something like a truncated normal distribution, which would implicitly shift the mean – Sam Mason Jul 07 '21 at 14:35
  • Maybe try solutions defined in this answer : Random number generator in Python when the mean is known -> https://stackoverflow.com/questions/55415799/random-number-generator-in-python-when-the-mean-is-known – Gorkem Jul 07 '21 at 14:39
  • @SamMason My experience with consulting and advising is that very often people's ability to define what they want is limited by their lack of familiarity with the subject matter. If somebody isn't familiar or comfortable with distributional concepts, they won't know whether or not a truncated normal is a good choice compared to other alternatives. – pjs Jul 07 '21 at 16:23
  • This is not a duplicate of the linked questions, those all specify there should be a particular total/sum. That means they are putting constraints on the realizations rather than the distribution. The average and the mean are two different things, the former is a function of a sample while the latter is a function of the distribution. Hammering this home, calculating the mean of a distribution requires no data, unlike calculating the sum or average of the data which, obviously, requires data. – pjs Jul 07 '21 at 17:33
  • @pjs agreed, your answer explains much better, I wanted to put some terms in that I hoped OP would be able to google for. I voted to close as wanting more details, shame stackoverflow picked a less useful reason here. – Sam Mason Jul 08 '21 at 08:20

1 Answers1

3

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)
pjs
  • 18,696
  • 4
  • 27
  • 56
  • The problem I have is that I want to make a probabilistic adjustment for the weibull, lognormal and beta functions. The problem is that I only have three values (maximum, minimum and average), would there be a way to generate random numbers and then carry out the probabilistic adjustment? – turaran32 Jul 12 '21 at 14:48
  • @turaran32 The Weibull and lognormal distributions have infinite ranges, so they're not determined by max/min except in the loose sense that sample min and max would correspond to estimated quantiles related to the sample size, but I wouldn't recommend that - the results would be very volatile if they're based on low-probability extremes. – pjs Jul 12 '21 at 15:40
  • @turaran32 If you have specific distributions in mind, or other constraints, you should edit your question to include those. That will also bring some attention to it and increase the chances of it being re-opened. Without specific details, it sounds like somebody who doesn't know enough about distributions to know what they want, and is looking for any sort of an approximate fix given the three characteristics. Adding detail helps others understand your needs better. – pjs Jul 12 '21 at 15:41