I have nothing to do, and recently I need to write down the user login to the homepage to discover cash red envelopes, and there is no limit. I just calculated how to write if there is a limit. I think this is similar to WeChat red envelopes. After the project requirements are completed. I happened to post the red envelope removal algorithm I wrote myself. I personally think this algorithm is more simulating the actual red envelope grab rules. Say less nonsense. Post the code first;
import java.math.BigDecimal;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;public class TestMain { public static void main(String[] args) { for (int i = 0; i < 10; i++) { List<BigDecimal> moneys = math(BigDecimal.valueOf(10), 6); if (moneys != null) { BigDecimal b = new BigDecimal(0); for (BigDecimal bigDecimal : moneys) { System.out.print(bigDecimal + "yuan"); b = b.add(bigDecimal); } System.out.print(" Total amount: " + b+"yuan"); System.out.println(); } } } /** * Calculate the amount of red envelopes received by each person; the minimum amount of red envelopes per person is 0.01 yuan per person* @param mm Total red envelopes* @param number Number of people* @return */ public static List<BigDecimal> math(BigDecimal mmmm, int number) { if (mmm.doubleValue() < number * 0.01) { return null; } Random random = new Random(); // Money, 10 yuan is calculated by fractions equal to 1000 points int money = mmmm.multiply(BigDecimal.valueOf(100)).intValue(); // Total random number double count = 0; // Each person gets random points double[] arrRandom = new double[number]; // Each person gets money List<BigDecimal> arrMoney = new ArrayList<BigDecimal>(number); // The number of people looping random points for (int i = 0; i < arrRandom.length; i++) { int r = random.nextInt((number) * 99) + 1; count += r; arrRandom[i] = r; } // Calculate the amount obtained by each person by opening the red envelope int c = 0; for (int i = 0; i < arrRandom.length; i++) { // Add up each person by adding random numbers to calculate the percentage of each person Double x = new Double(arrRandom[i] / count); // Each person obtains the amount obtained by each person by percentage int m = (int) Math.floor(x * money); // If 0 amount is obtained, set the minimum value of 1 cent if (m == 0) { m = 1; } // Calculate the total amount c += m; // If not the last person, calculate it normally if (i < arrRandom.length - 1) { arrMoney.add(new BigDecimal(m).divide(new BigDecimal(100))); } else { // If it is the last person, give the remaining money to the last person arrMoney.add(new BigDecimal(money - c + m).divide(new BigDecimal(100))); } } // Randomly disrupt the amount obtained by each person Collections.shuffle(arrMoney); return arrMoney; }}The thinking logic of this set of code:
1. First of all, we need to divide the red envelope amount into N shares based on the number of people, but because of the average value. If each person scores too evenly, it will seem to be playable.
2. Secondly, how should we distinguish? To ensure that the red envelopes are playable. Actually, I don't think these matter. After all, red envelopes only depend on luck. I was unlucky in playing WeChat red envelopes for 20 yuan and 3 people points. I also got 0.01 yuan. The concurrency will also be very large. So my set of codes simply put everyone out a random number (the range of random numbers is number of people * 100), add the random numbers of these people together and calculate the percentage of each person's random number. Distribute the dividends according to this percentage.
The above is the JAVA implementation simple red envelope grabbing algorithm introduced by the editor (simulating real red envelope grabbing). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!