An algorithm for generating red envelopes in Java _java

Source: Internet
Author: User

The new year is coming soon. Chinese New Year micro-letter red envelopes are very hot, recently have a project to do Rob red envelopes, so wrote a red envelope generation algorithm.

The demand of red envelope generation algorithm

Pre-generated all red envelopes or a request randomly generate a red envelope

In simple terms, it is the process of decomposing a large integer m (directly to "divided into units, such as 1 or 100) into n small integers, and the range of small integers is [min, Max]."

The simplest way of thinking, the first guarantee, each small red envelopes guaranteed to have min, and then each request randomly generated a 0 to (max-min) range of integers, plus min is the amount of red envelopes.

Although this algorithm is simple, but there is a disadvantage: the last generation of red envelopes may be the min money. That is to say, maybe the last red envelopes are 0.01 yuan.

Another way is to generate all the red envelopes beforehand, which makes it easier to control. I chose to generate all the red envelopes beforehand.

An ideal algorithm for red envelope generation

The ideal result of red envelopes is the average number of red envelopes nearby, the amount of large red envelopes and small red envelopes is relatively low.

It can be imagined that the distribution of the number of red envelopes is a bit like a normal distribution.

So how to achieve this average line near the value of more requirements?

is to find an algorithm that can increase the probability near the average. Then use a "swell" and "shrink" way to achieve this effect.

Square first, then generate the square range of random number, and then root, then the probability is no longer the average.

Specific algorithm:

public class Hongbaoalgorithm {static Random Random = new Random (); 
  static {Randomsetseed (Systemcurrenttimemillis ()); 
    public static void Main (string[] args) {long max = 200; 
 
    Long min = 1; 
    Long[] result = Hongbaoalgorithmgenerate (100_0000, 10_000, Max, Min); 
    Long total = 0; 
      for (int i = 0; i < resultlength i++) {//Systemoutprintln ("result[" + i + "]:" + result[i]); 
      Systemoutprintln (Result[i]); 
    Total + = Result[i]; 
 
    //Check whether the total amount of red envelopes generated is correct systemoutprintln ("sum:"); 
    Statistics the number of red envelopes per amount, check whether near normal distribution int count[] = new int[(int) max + 1]; 
    for (int i = 0; i < resultlength i++) {count[(int) result[i]] + = 1; 
    for (int i = 0; i < countlength i++) {systemoutprintln ("" + i + "" + count[i]); 
   }/** * produces random numbers between Min and Max, but the probability is not average, and the probability of direction from Min to Max increases. 
   * Square First, then produce a square value of the range of random number, and then the root, which produces a "swelling" and "contraction" effect. * @param min * @param mAX * @return/static long Xrandom (long, long max) {return sqrt (Nextlong (SQR (max-min))); /** * * @param total * Red Envelopes * @param count * @param max * The most of each small red envelope  Large * @param min * The minimum of each small red envelope * @return Store the value of each small red envelope generated by the array */public static long[] Generate (Long, 
 
    int count, long max, long min) {long[] result = new Long[count]; 
 
    Long average = Total/count; 
    Long a = average-min; 
 
    Long B = max-min; 
    The probability of such a random number is actually changed, and the probability of producing a large number is less than that of a decimal. This achieves the value of most red envelopes near the average. 
    Big red Envelopes and small red envelopes are relatively few. 
    Long Range1 = SQR (average-min); 
 
    Long Range2 = SQR (max-average); 
      for (int i = 0; i < resultlength i++) {//Because the number of small red envelopes is usually more than the number of large red envelopes, because the probabilities here are reversed.       When the random number > average, then the small red envelopes//when the random number < average, then produces the big red envelope if (Nextlong (min, max) > average) {//On average line cut money// 
        Long temp = min + sqrt (Nextlong (range1)); Long temp = min + xrandom(min, average); 
        Result[i] = temp; 
      Total = temp; 
        else {//Add money on the average line//Long temp = MAX-SQRT (Nextlong (range2)); 
        Long temp = Max-xrandom (average, max); 
        Result[i] = temp; 
      Total = temp; 
    If there is any money left, try adding it to the small red envelope and try the next one if you don't add it. while (Total > 0) {for (int i = 0; i < resultlength i++) {if (Total > 0 && result[i] & Lt 
          Max) {result[i]++; 
        total--; }///If the money is negative, it has to be taken back from the generated small red envelopes while (Total < 0) {for (int i = 0; i < resultlength; 
          i++) {if (Total < 0 && Result[i] > min) {result[i]--; 
        total++; 
  }} return result; 
    Static long sqrt (long n) {//improved to look-up table? 
  Return (long) mathsqrt (n); 
    Is the static long Sqr (long N) {//tabular fast, or directly? 
  return n * N; Static long Nextlong (long N) {return RandOmnextint ((int) n); 
  Static long Nextlong (long, long Max) {return randomnextint (int) (max-min + 1)) + min; 
 } 
}

Statistics the results of the next generation, or more in line with the requirements.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.