1

I am given a desired min, max, and length of an array. How can I generate numbers that fit to a "normally distributed" / bell curve for this array, with the specified min and max?

i.e.

min: 0 max: 6 length = 7

result: [0,2,4,6,4,2,0]

I know that I can linearly interpolate between the min and max to get to the middle, then in the reverse direction back down to the min at the end of the array. However, is there a way to do this using a distribution? then pull values from it?

i.e. I was thinking something like this

max - min  = diff
diff / (length/2) = increment
[min + increment*index, ..., max, max - increment*index, ..., min ] 
Riptyde4
  • 5,134
  • 8
  • 30
  • 57
  • There is no min and max for a bell curve. There is a mean and standard deviation. The tails technically go out to positive and negative infinity. Your output is more like a triangle waveform. – Cory Kramer Jun 22 '17 at 22:10
  • @CoryKramer right, understood. I want the array itself to MIMIC the shape of a bell curve if plotted. See my example – Riptyde4 Jun 22 '17 at 22:11
  • 1
    there's a problem in approximation because `[0,0,1,6,1,0,0]` is also a bell curve that fits the criteria – kmdreko Jun 22 '17 at 23:10

1 Answers1

2

If your problem is really to generate an array with values from a triangle shape, then there's not much more to do than the simple loop you suggested. You may even write a function which returns f(k). E.g like so:

double get_kth_value(double min, double max, int length, int k) {
    int mid = length/2;
    if (k < mid) {
         return min + (max - min) * k / mid;
    } else {
         return min + (max - min) * (length - 1 - k) / mid;
    }
}

When you say:

However, is there a way to do this using a distribution? then pull values from it?

I wonder whether you're hinting at the fact that your problem is slightly different. The wording suggest that you want to do sampling according to a given distribution. That is, you want to compute y=f(x) for x a uniform random variable, and the probability that you get a given y is prescribed by some given distribution (bell, binomial, triangle, whatnot). Then it becomes more fun (albeit super classical).

The generic sledgehammer for that is inverse transform sampling. You compute the cumulative distribution function, and there you go. For the case you suggest of a triangle shape, it's easy enough. Basically you'll want something like

double t = 2*uniformly_random_double_in_01()-1;
double y = breadth/2*(1-sqrt(1-fabs(1-t)))*(1-2*(t<0));

Pardon my laziness for not adjusting the bounds correctly, you need some of that stuff too, especially if you want integer values.

For the case of bell curves, there are various options:

  1. If you're satisfied with a no-brain approach, you might try the Box-Muller transform and truncate the result.
  2. If you're trying to obtain something which is related to a binomial distribution, then there are methods, too. See there.
EThome
  • 70
  • 6