Java implementation of a simple red envelope algorithm (simulation of the real robbery red envelopes) _java

Source: Internet
Author: User

Free to do nothing, the recent project needs to write a user login home to send cash red envelopes, there is no limit. I calculated myself a little bit if there was a limit to how to write. Think this is similar to the micro-letter red envelopes. After the completion of the project requirements. Just came to blog posted my own Write the algorithm of the red envelope. Personally think this algorithm compared to simulate the reality of the red envelope rules. Talk less. Paste 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 < i++) {List<big
      decimal> 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:" + B + "Yuan");
      System.out.println (); /** * Calculates the amount of red envelopes each person receives; minimum of $0.01 per person * @param mmm total red envelope * @param number * @return/public static list<bigdecimal> Math (BigDecimal mmm, int number) {if (Mmm.doublevalue () < number * 0.01) {return nul
    L
    } Random Random = new Random ();
    The money, calculated by 10 is equal to 1000 cents int ($) = Mmm.multiply (bigdecimal.valueof). Intvalue (); Total random number Double count = 0;
    Each person obtains random points double[] arrrandom = new Double[number];
    Each person obtains the amount of money list<bigdecimal> Arrmoney = new arraylist<bigdecimal> (number);
      Number of cycles random point for (int i = 0; i < arrrandom.length i++) {int r = Random.nextint ((number) * 99) + 1;
      Count = R;
    Arrrandom[i] = r;
    //Calculate each person to remove the red envelope to obtain the amount int c = 0; for (int i = 0; i < arrrandom.length i++) {//per person Gets the sum of the random numbers calculated per person% Double x = new double (Arrrandom[i]/cou
      NT);
      Each person obtains the amount by a percentage int m = (int) math.floor (x * money);
      If 0 amount is obtained, set a minimum value of 1 cents if (m = = 0) {m = 1;
      ///Calculate to obtain the total amount C = m; If it's not the last person, it's normal to compute if (I < arrrandom.length-1) {Arrmoney.add (new BigDecimal (M). Divide (new BigDecimal (10
      0)); else {//if the last person, give the remaining amount to the last person Arrmoney.add (new BigDecimal (Money-c + M). Divide (new BigDecimal (100))
      ;
   }///randomly upset each person to obtain the amount collections.shuffle (Arrmoney); return Arrmoney; }
}

The logic of this set of code:

1. First, we have to divide the red envelope amount into n according to the number of people, but because of the mean. It would be less fun if everyone were too evenly divided.

2. Second, how do we divide it? To ensure that the red envelope can play more. Actually, I don't think that's important. After all, red envelopes only look at luck. I play the micro-letter red envelopes bad luck 20 Yuan 3 person points I also smoked over 0.01 yuan. Concurrent volumes can also be very large. So my code is simply to give everyone a random number (a random number range is the number *100), add these people's random numbers together and then calculate the percentage of random numbers per person. Divide the red envelopes by this percentage.

The above is a small set to introduce the Java implementation of simple red envelope algorithm (simulate real robbery), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.