C # formula for number distribution

I am looking for a formula that can distribute numbers in a linear format based on the minimum number, maximum number and number of numbers (or points) between them. The trick is, the closer you get to the maximum, the more numbers should be there.

Example (the number will change and will be approximately 100 times larger)

Min = 0
Max = 16
AmountOfNumbersToSpread = 6

0 1 2 3 4 5 6 7 8 9 A B C D E F

1           2       3   4   5 6 

Thanks for the help in advance.

+1
source share
4 answers

Based on Tal Pressman's answer, you can write a distribution function as follows:

IEnumerable<double> Spread(int min, int max, int count, Func<double, double> distribution)
    {
    double start = min;
    double scale = max - min;
    foreach (double offset in Redistribute(count, distribution))
        yield return start + offset * scale;
    }

IEnumerable<double> Redistribute(int count, Func<double, double> distribution)
    {
    double step = 1.0 / (count - 1);
    for (int i = 0; i < count; i++)
        yield return distribution(i * step);
    }

You can use any distribution function that matches [0; 1] with [0; 1] in this way. Examples:

quadratic

Spread(0, 16, 6, x => 1-(1-x)*(1-x))

Output: 0 5.76 10.24 13.44 15.36 16

sinusoidal

Spread(0, 16, 6, x => Math.Sin(x * Math.PI / 2))

Output: 0 4.94427190999916 9.40456403667957 12.9442719099992 15.2169042607225 16
+2
source

Basically, you should have something similar:

  • 0 1.
  • ( 1:1 [0,1] → [0,1]).
  • .

, , , , , , , 1, 0. , sin cos.

+1

, :

MIN, MAX, AMOUNT:

Length = MAX - MIN
"mark" MIN and MAX
Length--, AMOUNT--
Current = MIN
While AMOUNT > 1
  Space = Ceil(Length * Amount / (MAX - MIN))
  Current += Space
  "mark" Current

"" - , .

0

, , .

List<int> lstMin = new List<int>();

int Min = 1;
int Max = 1500;

int Length = Max - Min;
int Current = Min;
int ConnectedClient = 7;
double Space;

while(ConnectedClient > 0)
{
    Space = Math.Ceiling((double)(Length * ConnectedClient / (Max - Min)));
    Current += (int)Space;

    ConnectedClient--;
    Length--;

    lstMin.Add(Current);
}
0

Source: https://habr.com/ru/post/1718351/


All Articles