在做一个项目的随机红包金额时,需要随机分配一个奖池的金额,查了其他人写的随机算法,要么分配极不平均,要么不能全部分完奖池,于是自己写了一个
核心思想是每次都按剩余的平均数随机,得到结果比较理想,记录防止忘记。
以下是代码
@Test
public void testRandom() throws Exception
{
int cnt = 12;
BigDecimal all = new BigDecimal("30");
for (int i = 0; i < 100; i++)
{
getRandom(cnt, all, false);
}
}
private void getRandom(int cnt, BigDecimal all, boolean pflag) throws Exception
{
all = all.multiply(new BigDecimal(100));
if (all.intValue() < cnt)
{
throw new Exception("金额错误啦");
}
List<BigDecimal> list = new ArrayList<>();
int res = 0;
for (int i = 0; i < cnt; i++)
{
int max = (all.intValue() - res) / (cnt - i) * 2 - 1;
int rand = 0;
if (cnt - i == 1)
{
rand = all.intValue() - res;
}
else
{
rand = new Random().nextInt(max) + 1;
}
if (rand <= 0)
{
throw new Exception("小于0啦");
}
if (pflag)
{
System.out.println(rand);
}
list.add(new BigDecimal(rand).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_DOWN));
res += rand;
}
System.out.println(list);
// System.out.println(cnt + " " + all + " " + res);
}