一个简单的红包分配实现
程序员文章站
2022-06-01 12:13:12
...
fun getRandomMoney(size: Int, totalMoney: Double): List<Double> {
var redPacketSize = size
var redPacketTotalMoney = totalMoney
val numList = ArrayList<Double>()
while (redPacketSize > 0) {
if (redPacketSize == 1) {
numList.add(Math.round(redPacketTotalMoney * 100).toDouble() / 100)
break
}
val min = 0.01
val max = redPacketTotalMoney / redPacketSize * 2
var 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
}
上一篇: 广度优先遍历