C#公式来分配号码 [英] C# Formula to distribute numbers

查看:126
本文介绍了C#公式来分配号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个公式,可以基于最小数,最大数和金额数(或点)之间的线性格式摊开数字。美中不足的是,你得到最大越近,更多的数字应该在那里。

I'm looking for a formula that can spread out numbers in a linear format based on a minimum number, max number and amount of numbers (or dots) between. The catch is, the closer you get to the max, the more numbers should be there.

这是例子(数量会有所不同,将是更大的约100倍)

An example (number will vary and will be about 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

感谢您的帮助提前

推荐答案

根据塔尔普雷斯曼的答案,你可以写这样的分布函数:

Based on the answer of Tal Pressman, you can write a distribution function like this:

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);
    }

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

You can use any kind of distribution function which maps [0;1] to [0;1] this way. Examples:

二次

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

这篇关于C#公式来分配号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆