JAVA implements rules for distributing fake WeChat Red Packets

Source: Internet
Author: User

JAVA implements rules for distributing imitation red packets

Recently, red envelopes have become a new trend. As programmers, their curiosity about algorithms is far greater than their curiosity about red envelopes, here we will introduce a random red packet distribution strategy that you have come up.

Algorithm Introduction

I. Red Envelope amount limit

For red packets, we know that no one random minimum red packet is 1 cent, and the maximum amount is 200 yuan. Here we also set the range of red packets. The following code defines the unit of money as minute.

 

// Minimum red packet quota private static final int MINMONEY = 1; // maximum red packet quota private static final int MAXMONEY = 200*100;

Ii. Determine whether the red envelope amount is valid

 

Note that this step is accompanied by the entire algorithm. We should not only determine whether the amount is valid before assigning a red packet, but also determine whether the remaining amount is valid after each person tentatively sets a random amount.

 

private boolean isRight(int money, int count) {double avg = money / count;if (avg < MINMONEY) {return false;}if (avg > MAXMONEY) {return false;}return true;}

3. Generate a random red envelope

 

Here we use a random method to generate a red envelope between MINMONEY and MAXMONEY. After a red envelope is generated, we need to determine whether the remaining money is a legal red envelope. If it is not a legal red envelope, we will re-generate the allocation scheme. When re-generate the allocation scheme, we need to determine whether the red packet is too large or too small. If the red packet is too large, next time, we will randomly generate a red envelope with a small value to the amount of this red envelope. If the amount of the red envelope is too small, we will generate a red envelope with the amount of the red envelope to the red envelope.

 

Private int random (int money, int minS, int maxS, int count) {// if (count = 1) {return money ;} // if the maximum amount is the same as the minimum amount, the return value is if (minS = maxS) {return minS;} int max = maxS> money? Money: maxS; // generate a random red envelope int one = (int) Math. rint (Math. random () * (max-minS) + minS) % max + 1; int money1 = money-one; // determine whether 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 a red envelope return random (money, minS, one, count);} else if (avg> MAXMONEY) {// recursive call, modify the minimum amount of a red envelope return random (money, one, maxS, count) ;}} return one ;}

4. Achieve red packet Distribution

 

To avoid a red envelope from occupying a large amount of money, we need to set the maximum amount of a non-last red envelope. We set it to N times the average value of the red envelope; with the one, two, and three methods, we can achieve the distribution of red packets.

 

// The maximum value of each red packet is a multiple of the average values: private static final double TIMES = 2.1; public List
 
  
SplitRedPackets (int money, int count) {if (! IsRight (money, count) {return null;} List
  
   
List = new ArrayList
   
    
(); // The maximum amount of the red packet is twice the average amount of int max = (int) (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 ;}
   
  
 

Red packet Distribution Scheme Evaluation

The basic algorithm of red packets is introduced above. Next we will verify the algorithm. Suppose there is a 200 yuan 100 red packets. Let's take a look at the final allocation 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 hongbao quota private static final int MINMONEY = 1; // maximum hongbao quota private static final int MAXMONEY = 200*100; // The maximum value of each red packet is the multiples of the average value: private static final double TIMES = 2.1;/*** @ param money * @ param count * @ return * @ Author: lulei * @ Description: split red packets */public List
 
  
SplitRedPackets (int money, int count) {if (! IsRight (money, count) {return null;} List
  
   
List = new ArrayList
   
    
(); // The maximum amount of the red packet is twice the average amount of int max = (int) (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 red packet quota */private int random (int money, int minS, int maxS, int count) {// if (count = 1) {return money;} // if the maximum and minimum amounts are the same, the return value is if (minS = maxS) {Return minS;} int max = maxS> money? Money: maxS; // generate a random red envelope int one = (int) Math. rint (Math. random () * (max-minS) + minS) % max + 1; int money1 = money-one; // determine whether 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 a red envelope return random (money, minS, one, count);} else if (avg> MAXMONEY) {// recursive call, modify the minimum amount of a red envelope return random (money, one, maxS, count) ;}} return one ;}/ ***** @ param money * @ param count * @ return * @ Author: lulei * @ Description: is this red envelope valid */private boolean 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 )));}}
   
  
 

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.