簡単なお年玉配分実現

4978 ワード

簡単なお年玉を実現し、 に合致するお年玉をランダムに生成します.
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
}