欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的红包分配实现

程序员文章站 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
}
相关标签: 随笔