The craziest thing I have played in recent years is to send red envelopes, especially during the Chinese New Year. I wrote a random algorithm for red envelopes below. It is actually quite simple. It only provides a way of thinking. I hope it can inspire you.
public class WxAlgorithm{ /** * @param moneySum Enter the total amount* @param redNum Enter the number of red packets*/ private static void wxAlgorithm(double moneySum, int redNum) { // Set the minimum amount double moneyMin = 0.01; Random random = new Random(); // Exact decimal point 2 digits NumberFormat formatter = new DecimalFormat("#.##"); for (int i=1;i<redNum;i++) { // A random number, the range of values is between the minimum value and the balance String money = formatter.format(random.nextDouble() * (moneySum - moneyMin) + moneyMin); //Numerical conversion moneySum = Double.valueOf(formatter.format(moneySum - Double.valueOf(money))); System.out.println("Th"+i+"red envelope: " + money + "yuan, balance: " + moneySum); } System.out.println("Last red envelope: " + moneySum + "yuan, balance: " + (moneySum - moneySum)); } public static void main(String[] args) { wxAlgorithm(10.26, 9); }}Later, some WeChat random red envelope algorithms were collected online for your reference.
public static double [] getMoney(double money, int num){ Random r = new Random(); DecimalFormat format = new DecimalFormat(".##"); double middle = Double.parseDouble(format.format(money/num)); double [] dou = new double[num]; double redMoney = 0; double nextMoney = money; double sum = 0; int index = 0; for(int i=num;i>0;i--){ if(i == 1){ dou[index] = nextMoney; }else{ while(true){ String str = format.format(r.nextDouble()*nextMoney); redMoney = Double.parseDouble(str); if(redMoney>0 && redMoney < middle){ break; } } nextMoney = Double.parseDouble(format.format(nextMoney - redMoney)); sum = sum + redMoney; dou[index] = redMoney; middle = Double.parseDouble(format.format(nextMoney/(i-1))); index++; } } return dou; } Here is the basic idea: first calculate the average value of the red envelope, and then pass a red envelope with a random number of red envelopes smaller than this average value. After the red envelope is sent, the total amount of the red envelope needs to be reduced accordingly. At the same time, recalculate the average: the new total amount % (original total red envelope number -1), until the last red envelope, put all the remaining amount in.
There are also many exciting source code sharing: Java's simple random red envelope allocation algorithm to implement source code.
package com.sunron.test;import java.math.BigDecimal;import org.junit.Test;public class HongBao { @Test public void testHonbao(){ hb(100, 9, 0.01);//Amount, number, minimum value//zb(); } void hb(double total,int num,double min){ for(int i=1;i<num;i++){ double safe_total=(total-(num-i)*min)/(num-i); double money=Math.random()*(safe_total-min)+min; BigDecimal money_bd=new BigDecimal(money); money=money_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); total=total-money; BigDecimal total_bd=new BigDecimal(total); total=total_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println("Th"+i+"red envelope: "+money+", balance is: "+total+"yuan"); } System.out.println("th"+num+" red envelope: "+total+", balance is: 0 yuan"); } void zb(){ for(int a=0;a<=10000;a++){ if(a % 1000== 0) System.out.println (a); } }}When others are grabbing red envelopes, we will study the algorithm for random red envelopes:
public static void main(String[] args) { //The number of red envelopes int number = 10; //The total amount of red envelope float total = 100; float money; //The minimum red envelope double min = 1; double max; int i = 1; List math = new ArrayList(); DecimalFormat df = new DecimalFormat("##.##"); while (i < number) { //Ensure that even if a red envelope is the largest, each red envelope will not be less than the minimum value of the remaining red envelopes max = total - min * (number - i); int k = (int)(number - i) / 2; //Make sure that the red envelopes received by the last two people do not exceed the remaining red envelopes if (number - i <= 2) { k = number - i; } //The average limit of the maximum red envelope is max = max / k; //Make sure that each red envelope is greater than the minimum value and will not be greater than the maximum value money = (int) (min * 100 + Math.random() * (max * 100 - min * 100 + 1)); money = (float)money / 100; //Retain two decimal places money = Float.parseFloat(df.format(money)); total=(int)(total*100 - money*100); total = total/100; math.add(money); System.out.println("Thread" + i + "Individual get" + money + "Left" + total); i++; //The last person takes away the remaining red envelope if (i == number) { math.add(total); System.out.println("Thread" + i + "Individual get" + total + "Left 0"); } }//Selection of the index of the largest value in the array System.out.println("Thread in this round of red envelopes" + (math.indexOf(Collections.max(math)) + 1) + "The best personal luck"); }For more exciting content, please click "Android WeChat Development Tutorial Summary" and "Java WeChat Development Tutorial Summary" welcome everyone to learn and read.
The above are the various WeChat random red envelope algorithms shared by you, which provide you with a variety of ideas. I hope it will be helpful for everyone to learn to use the WeChat random red envelope algorithm. I also hope that you will continue to pay attention to more exciting content from Wulin.com.