C#分配号码的公式

我正在寻找一个公式,可以根据最小数量,最大数量和数字(或点)之间的数字以线性格式展开数字。 问题是,越接近最大值,那里的数字就越多。

一个例子(数字会有所不同,大约会增加100倍)

Min = 0 Max = 16 AmountOfNumbersToSpread = 6 0 1 2 3 4 5 6 7 8 9 ABCDEF 1 2 3 4 5 6 

我在这里先向您的帮助表示感谢。

根据Tal Pressman的答案,您可以编写如下分布函数:

 IEnumerable Spread(int min, int max, int count, Func distribution) { double start = min; double scale = max - min; foreach (double offset in Redistribute(count, distribution)) yield return start + offset * scale; } IEnumerable Redistribute(int count, Func distribution) { double step = 1.0 / (count - 1); for (int i = 0; i < count; i++) yield return distribution(i * step); } 

您可以使用以这种方式映射[0; 1]到[0; 1]的任何类型的分布函数。 例子:

二次

 Spread(0, 16, 6, x => 1-(1-x)*(1-x)) Output: 0 5.76 10.24 13.44 15.36 16 

正弦

 Spread(0, 16, 6, x => Math.Sin(x * Math.PI / 2)) Output: 0 4.94427190999916 9.40456403667957 12.9442719099992 15.2169042607225 16 

基本上,你应该有一些看起来像:

  1. 生成0到1之间的随机数。
  2. 实现所需的分布函数([0,1] – > [0,1]中的1:1函数)。
  3. 缩放分布函数的结果以匹配您想要的范围。

用于第二个点的确切函数是根据您希望如何分配数字来确定的,但根据您的要求,您将需要一个具有接近于1的值的函数。例如,犯罪或cos函数。

在纸上试过这个并且它有效:

给定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 

通过“标记”我的意思是选择那个数字,或者你需要做什么。

尽管如此,关闭答案需要适用于更大的数字。

 List lstMin = new List(); 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); }