Java implementation of the _java of imitation micro-letter red envelope allocation rules

Source: Internet
Author: User

Recent Chinese New Year red envelopes have become a novel trend, as a program ape on the algorithm is more curious than the curiosity of red envelopes, here introduced a kind of random red envelopes of their own thinking, and please advise.

Algorithm Introduction

One, the amount of red envelopes limit

For the micro-letter red envelopes, we know that no one random minimum red envelope is 1 points, the maximum amount is 200 yuan, here we also set the scope of red envelopes, the following code we unified the unit of money for the cent.

Minimum red envelope amount 
private static final int minmoney = 1; 
Maximum Bonus amount 
private static final int maxmoney = 200 * 100; 

Second, to determine whether the amount of red envelopes is legal
Note that this step along with the entire algorithm, we should not only in the distribution of red envelopes before we judge whether the amount is legitimate, but also to the provisional random amount of each person to determine whether the remaining amount is legal.

Private Boolean isright (int money, int count) { 
 double avg = money/count; 
 if (avg < Minmoney) {return 
 false; 
 } 
 if (avg > Maxmoney) {return 
 false; 
 } 
 return true; 
} 

Three, randomly produce a red envelope
Here we use a random way to generate a red envelope between Minmoney and Maxmoney, after which we need to determine whether the remaining money is a legal red envelope, and if it is not a legal red envelope, we will redistribute the distribution, and when the distribution is redistributed, We need to determine a thing, is the resulting red envelopes too large or too small, if the red envelope is too large, the next time a small value to the amount of a red envelope, if the amount of red envelopes is too small, we will produce a red envelope amount to a large value of a red envelope.

private int random (int money, int mins, int maxs, int count) { 
 //Red envelope quantity is 1, direct return amount 
 if (count = = 1) {returns money 
 ; 
 } 
 If the maximum amount and the minimum amount are equal, the direct return amount if 
 (mins = = Maxs) {returns 
 mins; 
 } 
 int max = MAXS > Money? MONEY:MAXS; 
 Randomly generate a red envelope 
 int one = ((int) math.rint (Math.random () * (max-mins) + mins))% max + 1; 
 int money1 = Money-one; 
 Determine if the allocation scheme is correct 
 if (Isright (Money1, count-1)) {return one 
 ; 
 } else { 
 double avg = money1/(count-1) ; 
 if (avg < Minmoney) { 
  //recursive call, modify the maximum amount of red envelopes return 
  random (money, mins, one, count); 
 } else if (avg > Maxmoney) { 
  //recursive call, modify the minimum amount of red envelope return 
  random (money, one, MAXS, count); 
 } 
 return one; 
} 

Iv. realizing the distribution of red envelopes
in order to avoid a certain red envelope to occupy a lot of money, we need to set the maximum amount of not the last red envelope, we set him to the average amount of red envelopes n times; with one or two or three of the methods, we can achieve the distribution of red envelopes.

The maximum of each red envelope is a multiple of the average. 
private static final double times = 2.1; 
 
Public list<integer> splitredpackets (int $, int count) { 
 if (!isright (Money, Count)) {return 
 null;
   } 
 list<integer> List = new arraylist<integer> (); 
 The maximum amount of red envelopes is the Times 
 int max = (int) of the average amount (money * times/count); 
 max = max > Maxmoney? Maxmoney:max; 
 for (int i = 0; i < count; i++) { 
 Int. one = random (Money, Minmoney, Max, count-i); 
 List.add (one); 
 Money-= one; 
 } 
 return list; 
} 

Evaluation of red envelope allocation scheme

The above introduced the basic algorithm of red envelopes, we have a verification of the algorithm, assuming that there is a 200 yuan 100 red envelopes, we look at the final distribution scheme.

Complete code

 /** * @Description: * * Package com.lulei.weixin.util; 
Import java.util.ArrayList; 
 
Import java.util.List; 
 
Import Com.lulei.util.JsonUtil; 
 public class Redpacketutil {//Minimum red envelope amount private static final int minmoney = 1; 
 Maximum Bonus amount private static final int maxmoney = 200 * 100; 
 
 The maximum of each red envelope is a multiple of the average. private static final double times = 2.1; /** * @param Money * @param count * @return * @Author: Lulei * @Description: Split red envelope/public LIST&LT;INTEGER&G T 
 Splitredpackets (int, count) {if (!isright, count)) {return null; 
 } list<integer> List = new arraylist<integer> (); 
 The maximum amount of red envelopes is the Times int max = (int) of the average amount (money * times/count); max = max > Maxmoney? 
 Maxmoney:max; 
 for (int i = 0; i < count; i++) {Int. one = random (Money, Minmoney, Max, count-i); 
 List.add (one); 
 Money-= one; 
 } return list; 
 /** * @param money * @param mins * @param maxs * @param count * @return * @Author: Lulei* @Description: Random Bonus Amount * */private int random (int money, int mins, int maxs, int count) {//Red envelope number 1, direct return amount if (count 
 = = 1) {return money; 
 //If the maximum amount and the minimum amount are equal, the direct return amount if (mins = = maxs) {returns mins; int max = MAXS > Money? 
 MONEY:MAXS; 
 Randomly generate a red envelope int one = ((int) math.rint (Math.random () * (max-mins) + mins))% max + 1; 
 int money1 = Money-one; 
 Determine if the allocation scheme is correct if (Isright (Money1, count-1)) {return one; 
 else {Double avg = money1/(count-1); 
 if (avg < Minmoney) {//Recursive call, modify the maximum amount of red envelopes return random (money, mins, one, count); 
 }else if (avg > Maxmoney) {//Recursive call, modify the minimum amount of red envelope return random (money, one, MAXS, count); 
 } return one; /** * @param Money * @param count * @return * @Author: Lulei * @Description: Whether this kind of red envelope is legal/private Boolea 
 n isright (int money, int count) {double avg = money/count; 
 if (avg < Minmoney) {return false; 
 } if (Avg > Maxmoney) {return false; 
 return true; 
 
 }public static void Main (string[] args) {//TODO auto-generated method stub redpacketutil util = new Redpacketutil (); 
 System.out.println (Jsonutil.parsejson (util.splitredpackets (20000, 100));  } 
}

The above is the entire content of this article, I hope to learn Java program to help you.

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.