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; } }
微信抢红包算法
最新推荐文章于 2024-05-21 16:32:29 发布