Generate a distributed set of N numbers between a given min and max, with a given mean.

588 Views Asked by At

Is it possible to generate a set of N numbers between a given min and max that will average to a given mean.

eg-1. Generate a set of 30 numbers from 20 to 650 with an average (mean) of 260.

eg-2. Generate a set of 51 numbers from 360 to 8746 with an average (mean) of 2714.

The resulting set of numbers should rise fairly quickly toward the mean, then flatten out before rising again quickly towards the max. (I think this is called an S-shaped distribution?)

S shaped curve

1

There are 1 best solutions below

2
On

One possibility:

  • Let $\mu$ denote the target mean value
  • Draw $N$ numbers from a normal distribution with zero mean and variance $\sigma^2$.
  • You can control $\sigma$ to manage the tightness of the distribution. Pick $\sigma = (\max - \min) / \sigma$ to span the entire range.
  • Subtract the mean of the $N$ random numbers, so that they have zero mean
  • Add the mean adjusted random numbers to $\ mu$.
  • Discard any values outside your desired range $[\min, \max]$.
  • Repeat the process until you have $N$ numbers that satisfy all your constraints

In the example below, I throw out previously generated numbers, but you can choose to keep and only generate data incrementally:

# Inputs
min = 20
max = 650
ave = 260
num = 30

# Derived param
sigma = (max - min) / 6

# Algorithm
while True:
    r = np.random.randn(num) * sigma
    r = r - np.mean(r)
    y = r + ave
    y = y[(y >= min) & (y <= max)]
    if len(y) == num:
        break