一个简单的红包分配实现

【一个简单的红包分配实现】一个简单的红包实现,随机生成一些符合正态分布的红包。

fun getRandomMoney(size: Int, totalMoney: Double): List { var redPacketSize = size var redPacketTotalMoney = totalMoney val numList = ArrayList() while (redPacketSize > 0) { if (redPacketSize == 1) { numList.add(Math.round(redPacketTotalMoney * 100).toDouble() / 100) break } val min = 0.01 val max = redPacketTotalMoney / redPacketSize * 2var normalR = -1.0 while (normalR < 0 || normalR > 1) normalR = Math.sqrt(0.01) * r.nextGaussian() + 0.5 var money = normalR * max money = if (money <= min) min else money money = Math.floor(money * 100) / 100 redPacketSize-- redPacketTotalMoney -= money numList.add(money) } return numList }

    推荐阅读