0

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

  • Hi: your question is interesting but I don't think it's possible ( I guess someone else could come up with something so don't take this as word ) because you are trying to generate random numbers that have a deterministic constraint. If you satisfy the constraint, then your numbers aren't random. – mark leeds Sep 19 '22 at 14:29
  • There are many ways to do this. You could sample from a normal distribution and (if you want) reject values outside your desired range and (if you want) pick the last number to get the exact desired mean. – Karl Sep 19 '22 at 14:36
  • Different ways of doing this can give you different standard deviations and other properties of the data set, so the right way depends on what you're using it for. – Karl Sep 19 '22 at 14:47

1 Answers1

0

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

Godfather
  • 2,355
  • Thank you - This is close, but I'd prefer if the distribution was calculated, not random. In other words, the same inputs should always result in the same output.

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

    Perhaps done in two steps? First, generate half the numbers (min - mean), and then the other half (mean - max). Two generated S-shaped distributions?

    – Madmutt Sep 21 '22 at 10:12
  • Using the examples above,

    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.

    I need to derive a formula that will give me an S-shaped curve.

    The x-values would go from 0 to n numbers and the y-values from min to max. The generated set of numbers should have an average of mean.

    I think this should be a sigmoid function or logistic function - or some type of calculus derivative or based formula.

    – Madmutt Sep 21 '22 at 10:24