一个简单的金额平均分配函数(C#版)

时间:2022-09-29 11:15:06

//总金额平均分配给总人数

//参数说明:总金额,总人数,最大金额为平均金额的倍率

public double[] GetList(double zje,int zrs,int max)
{
double total = zje;//总金额
int count = zrs;//总人数
double avg = total / count;
double[] list = new double[count];
int seed = 1;
while (count > 0)
{
if (count == 1)
{
list[zrs- count] = Math.Round(total,2);
count--;
}
else
{
Random r = new Random(seed);
double lv = (double)r.Next(max*100) / 100;
if (total - avg * lv <= 0)
{
seed++;
continue;
}
list[60 - count] = Math.Round(avg * lv,2,MidpointRounding.AwayFromZero);
total -= Math.Round(avg * lv, 2, MidpointRounding.AwayFromZero);
count--;
}
seed++;
}
return list;
}