A tool that supports real-time calculation of random red packets, support maximum integer.max

Refer to the link: www.cnblogs.com/hanganglin/…

package com.example.demo; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * @author zhanglu * @date Created in 2020/8/18 14:45 * @description * @modifiedBy * @version: */ public class hongbao4 {/** * return public static Boolean ** @param rate * @return */ canReward(double rate) { return Math.random() <= rate; } /** * returns a random number in the min~ Max range, Private static int getRandomVal(private static int getRandomVal(private static int getRandomVal(private static int getRandomVal)) int max) { Random random = new Random(); return random.nextInt(max - min + 1) + min; Private static Boolean isRight(int money, int count, int count); int min, int max) { double avg = money / count; If (avg < min) {return false; } else if (avg > Max) {return false; } return true; } /** * Random algorithm with probability bias, probability bias subMin~subMax interval * Returns random numbers in boundMin~boundMax interval (including boundMin and boundMax), and can specify the priority probability of subMin~subMax interval * example: Pass in the parameter (10, 50, 20, 30, 0.8), then the random result has 80% probability to return randomly from 20~30, There is a 20% chance of randomly returning @param boundMin bounds from 10 to 50 * @param boundMax * @param subMin * @param subMax * @param subRate * @return */  public static int getRandomValWithSpecifySubRate(int boundMin, int boundMax, int subMin, int subMax, double subRate) { if (canReward(subRate)) { return getRandomVal(subMin, subMax); } return getRandomVal(boundMin, boundMax); } /** * randomly allocate the NTH bonus ** @param totalNum * @param totalNum * @param sendedBonus * @param sendedNum * @param sendedNum * @ param rdMin random limit * @ param rdMax random limit * @ return * / private static Integer randomBonusWithSpecifyBound (Integer totalBonus, Integer totalNum, Integer sendedBonus, Integer sendedNum, Integer rdMin, Integer rdMax, double bigRate) { if (! isRight(totalBonus, totalNum, rdMin, rdMax)) { return null; } Integer avg = totalBonus / totalNum; // Average value Integer leftLen = avg-rdmin; Integer rightLen = rdMax - avg; Integer boundMin = 0, boundMax = 0; If (leftlen.equals (rightLen)) {boundMin = math.max ((totalBonus - sendedBonus - (totalNum - sendedNum - 1) *  rdMax), rdMin); boundMax = Math.min((totalBonus - sendedBonus - (totalNum - sendedNum - 1) * rdMin), rdMax); } else if (rightLen.compareTo(leftLen) > 0) {// Upper bound deviated Integer standardRdMax = avg + leftLen; Integer _rdMax = canReward(bigRate)? rdMax : standardRdMax; boundMin = Math.max((totalBonus - sendedBonus - (totalNum - sendedNum - 1) * standardRdMax), rdMin); boundMax = Math.min((totalBonus - sendedBonus - (totalNum - sendedNum - 1) * rdMin), _rdMax); } else {// Lower limit deviated Integer standardRdMin = avg-rightlen; Integer _rdMin = canReward(bigRate)? rdMin : standardRdMin; boundMin = Math.max((totalBonus - sendedBonus - (totalNum - sendedNum - 1) * rdMax), _rdMin); boundMax = Math.min((totalBonus - sendedBonus - (totalNum - sendedNum - 1) * standardRdMin), rdMax); } return getRandomVal(boundMin, boundMax); return getRandomVal(boundMin, boundMax); } double currAvg = sendedNum == 0 ? (double) avg : (sendedBonus / (double) sendedNum); Double middle = (boundMin + boundMax) / 2.0; Integer subMin = boundMin, subMax = boundMax; Double exp = avg - (currAvg - avg) * sendedNum/(totalNum - sendedNum); If (middle > exp) {subMax = (int) Math. Round ((boundMin + exp) / 2.0); } else {subMin = (int) Math. Round ((exp + boundMax) / 2.0); } Integer expBound = (boundMin + boundMax) / 2; Integer expSub = (subMin + subMax) / 2; double subRate = (exp - expBound) / (double) (expSub - expBound); return getRandomValWithSpecifySubRate(boundMin, boundMax, subMin, subMax, subRate); @param totalNum totalBonus * @param rdMin random maximum bonus * @param rdMax random minimum bonus * @return */ public static List<Integer> createBonusList(Integer totalBonus, Integer totalNum, Integer rdMin, Integer rdMax, double bigRate) { Integer sendedBonus = 0; Integer sendedNum = 0; List<Integer> bonusList = new ArrayList<>(); while (sendedNum < totalNum) { Integer bonus = randomBonusWithSpecifyBound(totalBonus, totalNum, sendedBonus, sendedNum, rdMin, rdMax, bigRate); if (null == bonus) { return bonusList; } bonusList.add(bonus); sendedNum++; sendedBonus += bonus; } return bonusList; } public static void main(String[] arg) {List<Integer> bonusList = createBonusList(20000, 200, 1, 500, 0.8); System.out.println(bonusList); Integer totle = 0; for (Integer i : bonusList) { totle = totle + i; } system.out. println(" total amount of money sent: "+ totle); }}Copy the code