1. 程式人生 > >微信搶紅包演算法

微信搶紅包演算法

package com.das.controller;
import java.util.Random;
public class Test {
    public static void main(String[] args) {
        RedPackage redPackage = new RedPackage();
redPackage.remainSize  = 3;
redPackage.remainMoney = 50;
        for(int i=0;i<3;i++) {
            System.out.println(getRandomMoney
(redPackage)); } } public static double getRandomMoney(RedPackage _redPackage) { // remainSize 剩餘的紅包數量 // remainMoney 剩餘的錢 if (_redPackage.remainSize == 1) { _redPackage.remainSize--; return (double) Math.round(_redPackage.remainMoney * 100) / 100
; } Random r = new Random(); double min = 0.01; // double max = _redPackage.remainMoney / _redPackage.remainSize * 2; double money = r.nextDouble() * max; money = money <= min ? 0.01: money; money = Math.floor(money * 100) / 100; _redPackage.remainSize--; _redPackage.remainMoney
-= money; return money; } }