Micro-letter random generation of red envelope amount algorithm Java version _java

Source: Internet
Author: User

In recent years to play the most crazy should be red envelopes, especially when the new Year is particularly popular, the following write a red envelope of the random algorithm, in fact quite simple, is to provide a way of thinking, hope to give you some inspiration.

public class wxalgorithm{

 /**
 * @param moneysum Enter total amount
 * @param rednum Enter the number of red envelopes * *
 private static void W Xalgorithm (double moneysum, int rednum) {
 //Set smallest amount
 double moneymin = 0.01;
 Random Random = new Random ();
 Exact decimal point 2-bit
 numberformat formatter = new DecimalFormat ("#.##");
 for (int i=1;i<rednum;i++)
 {
   //random number, numeric range between minimum value and balance String money
   = Formatter.format ( Random.nextdouble () * (moneysum-moneymin) + moneymin);
   numeric conversion
       moneysum = double.valueof (Formatter.format (moneysum-double.valueof (Money));
  
   System.out.println ("The" "+i+" a 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 on the internet to collect a number of micro-letters randomly generated red envelope algorithm for everyone's reference.

 public static double [] Getmoney (double, 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; }

One of the basic ideas here is this: first calculate the average value of red envelopes, and then through a random red packets less than the average number of red envelopes, when the red envelopes, the total amount of red envelopes need to be correspondingly reduced, and recalculate the average: The new total amount (the original total red envelope number-1), until the last red envelope, Cram all the remaining money into it.

There are many wonderful source sharing: Java simple to rob red envelopes random allocation of red envelopes algorithm to achieve the 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 ("The" "+i+" a red Envelope: "+money+", the balance is: "+total+");
 }
 System.out.println ("+num+" "a red Envelope:" +total+ ", the balance is: 0 Yuan");
 }
 
 void Zb () {for
     (int a=0;a<=10000;a++) {
      if (a% 1000== 0) 
      System.out.println (a);
     }

}}

When someone is robbing a red envelope, we're going to study the random red Envelope algorithm:

public static void Main (string[] args) {//red packets int number = 10;
    Total amount of red envelopes = 100;
    float money;
    Minimum red packet double min = 1;
    Double Max;
    int i = 1;
    List math = new ArrayList ();
    DecimalFormat df = new DecimalFormat ("###.##");
      while (I < number) {//Guarantee even if a red envelope is the largest, the remaining red envelopes, each red envelope will not be less than the minimum value max = Total-min * (number-i);
      int k = (int) (number-i)/2;
      Ensure that the last two persons take the red envelopes not exceeding the remaining red envelopes if (Number-i <= 2) {k = number-i;
      //maximum red envelope limit of the average line up and down max = max/k;
      Ensure that each red envelope is greater than the minimum value and will not be greater than the maximum money = (int) (min * + math.random () * (max * 100-min * 100 + 1));
      Money = (float) money/100;
      Reserve two decimal money = Float.parsefloat (Df.format);
      total= (int) (TOTAL*100-MONEY*100);
      Total = total/100;
      Math.add (Money);
      System.out.println ("First" + i + "personal get" + Money + "left" + total);
      i++;
 The last person to take the remaining red envelopes if (i = = number) {Math.add (total);       System.out.println ("First" + i + "personal get" + total + "left 0");
  }//Fetch the index of the largest value in the array System.out.println ("The first in this round of red envelopes" + (Math.indexof (Collections.max (math) + 1) + "Personal luck best"); }

The above is for you to share a variety of micro-letter random red envelope algorithm, for you to provide a variety of ideas, I hope to learn how to use micro-letter random red envelope algorithm to help, but also hope that we continue to pay attention to the cloud-dwelling community more wonderful content.

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.