简体   繁体   中英

Creating a curve given min, max, and length

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?

ie

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?

ie I was thinking something like this

max - min  = diff
diff / (length/2) = increment
[min + increment*index, ..., max, max - increment*index, ..., min ] 

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) . Eg 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 xa 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM