People are stealing red envelopes, programmers are studying red envelopes. _php instance of the algorithm

Source: Internet
Author: User
Tags truncated

New Year's Day micro-letter users of the total number of red envelopes sent up to 1.01 billion times, shake the interaction reached 11 billion times, red packets peak sent 810 million times/minute.

Put aside the market value of micro-letter red envelopes do not talk about the red envelope itself, the algorithm also triggered a hot discussion, because the official did not give a clear statement, the family is also different opinions, small series below also for everyone to bring several analysis.

First look at the data analysis Emperor

Most people make their own guesses, which is the only option when they don't know about the internal stochastic algorithm, but most people don't give their own findings. Here is a sample of 100 samples of the survey sample data, and put forward their own guess.

1. The wallet amount satisfies the truncated normal random number distribution. In general, the random number is taken in the truncated normal distribution, the sum is divided by the total value, the correction factor is obtained, and the correction factor is multiplied by all the random numbers to get the value of the red envelope.

This distribution means that there are more red envelopes below the average, but not far from the average, with less than the average, but far more than the average red envelopes.


Figure 1. Wallet value and its frequency distribution histogram and its normal fitting

But looking at the distribution histogram can not be introduced it conforms to the normal distribution, but considering the simplicity of the program and the rationality of random number, this is the most reasonable guess.
The more behind the purse, the higher the value generally


Figure 2. The relationship between wallet sequence number and its value curve

From the linear fitting red line in Figure 2, we can see that the overall trend of wallet value is gradually increasing, and the range of change is about the "channel" drawn on the lower bound of a green dash. (curves can be enclosed in such a regular "channel", and also reflect the rationality of rule 1 from the side, indicating that the random number is not evenly distributed)
This rule can also be seen from the graph of another average.


Figure 3. The variation curve of average with sequence number

In the sample, 1000 of the value of the wallet was divided into 100 parts, the mean value of 10. In Figure 3, however, we can see that before the last wallet, the average has been below 10, which means that the value of the wallet at the beginning is low and has been pulled upwards by the value of the wallet later, with a higher value for the wallet later.

3. The average figure can also reveal another rule, that is, the last person is apt to get lucky and smoke more. Because the last person is the wallet to take how much, and everyone before the average is less than 10, so at least to ensure that the last person will be above average. In this sample, purse number 98th is drawn to 35 and the last wallet is 46.

In summary, according to the sample guess:


1. Most of the money you draw is as small as anyone else, but once you get more, it's much easier.
2. The more money you draw behind, the easier it is to get more.
3. The last person is often easily luck.

Comment on: This obviously very practical difference, small knitting every time whenever Rob is a few cents.

The second classmate wrote a simple python code

According to observation, the red envelopes meet the following points:

1. No one's going to get the money.

2. Do not finish in advance

3. Money fluctuations in a wide range

When the red envelope was first created, the distribution was set. When you grab a red envelope, it's just pop up.

So the Python code is as follows:

def Weixin_divide_hongbao (Money, N): 
divide_table = [Random.randint (1, 10000) for
x in xrange (0, N)] 
sum_ = s Um (divide_table) 

But there are two small problems with these algorithms:

1. Floating-point precision problem

2. Processing of boundary value

The third student wrote a Java version of Python, which was circulated on the Internet.

int j=1; 
while (j<1000) 
{ 
int number=10; 
float total=100; 
float money; 
Double min=0.01; 
Double Max; 
int i=1; 
 
List math=new ArrayList (); 
while (I<number) 
{ 
 
max = total-min* (number-i); 
int k = (int) ((number-i)/2); 
if (number-i <= 2) 
{k = number-i;} 
max = max/k; 
money= (int) (Min*100+math.random () * (max*100-min*100+1)); 
money= (float) money/100; 
Total=total-money; 
Math.add (money); 
System.out.println ("+i+" "The individual Gets" +money+ "left" +total); 
i++; 
if (I==number) 
{ 
math.add (total); 
System.out.println ("+i+" "The individual Gets" +total+ "remaining 0"); 
} 
 
System.out.println ("The first in this round of red envelopes" + (Math.indexof (Collections.max (math)) + +1) + "personal luck best"); 
j + +; 
}

This algorithm of the fourth classmate looks very scientific.

He thought:

1, everyone should be able to receive red envelopes;

2, the sum of the amount of red envelopes each person receives = total amount;

3, each person to receive the amount of red envelopes, but can not be too unreasonable, otherwise there is no fun;

4, the algorithm must be simple, or I'm sorry Tencent this sign;

Before the formal coding, a progressive model is set up to analyze the law.

Set the total amount of 10 yuan, there are n individuals to collect randomly:

N=1

The red envelope amount =x yuan;

n=2

To ensure that the second red envelope can be issued normally, the first red envelope amount = a random number between 0.01 and 9.99

The second red envelope =10-the first red envelope amount;

N=3

A random number between red envelopes 1=0.01 to 0.98

A random number of red envelopes 2=0.01 to (10-red envelope 1-0.01)

Red Envelopes 3=10-Red Envelopes 1-Red Envelopes 2

......

int j=1; 
while (j<1000) 
{ 
int number=10; 
float total=100; 
float money; 
Double min=0.01; 
Double Max; 
int i=1; 
 
List math=new ArrayList (); 
while (I<number) 
{ 
 
max = total-min* (number-i); 
int k = (int) ((number-i)/2); 
if (number-i <= 2) 
{k = number-i;} 
max = max/k; 
money= (int) (Min*100+math.random () * (max*100-min*100+1)); 
money= (float) money/100; 
Total=total-money; 
Math.add (money); 
System.out.println ("+i+" "The individual Gets" +money+ "left" +total); 
i++; 
if (I==number) 
{ 
math.add (total); 
System.out.println ("+i+" "The individual Gets" +total+ "remaining 0"); 
} 
 
System.out.println ("The first in this round of red envelopes" + (Math.indexof (Collections.max (math)) + +1) + "personal luck best"); 
j + +; 

Enter a look, fluctuation is too big, this data is too boring!

The 1th red envelope: 7.48 yuan, the balance: 2.52 yuan

The 2nd Red Envelope: 1.9 Yuan, the balance: 0.62 yuan

The 3rd Red envelope: 0.49 Yuan, the balance: 0.13 yuan

The 4th Red Envelope: 0.04 Yuan, the balance: 0.09 yuan

The 5th Red envelope: 0.03 Yuan, the balance: 0.06 yuan

The 6th Red envelope: 0.03 Yuan, the balance: 0.03 yuan

The 7th Red Envelope: 0.01 Yuan, the balance: 0.02 yuan

The 8th Red Envelope: 0.02 Yuan, the balance: 0 yuan

To improve it, use the mean as a random security cap to control volatility

int j=1; 
while (j<1000) 
{ 
int number=10; 
float total=100; 
float money; 
Double min=0.01; 
Double Max; 
int i=1; 
 
List math=new ArrayList (); 
while (I<number) 
{ 
 
max = total-min* (number-i); 
int k = (int) ((number-i)/2); 
if (number-i <= 2) 
{k = number-i;} 
max = max/k; 
money= (int) (Min*100+math.random () * (max*100-min*100+1)); 
money= (float) money/100; 
Total=total-money; 
Math.add (money); 
System.out.println ("+i+" "The individual Gets" +money+ "left" +total); 
i++; 
if (I==number) 
{ 
math.add (total); 
System.out.println ("+i+" "The individual Gets" +total+ "remaining 0"); 
} 
 
System.out.println ("The first in this round of red envelopes" + (Math.indexof (Collections.max (math)) + +1) + "personal luck best"); 
j + +; 
}

The output results are shown in the following figure

The 1th red envelope: 0.06 Yuan, the balance: 9.94 yuan

The 2nd red envelope: 1.55 Yuan, the balance: 8.39 yuan

The 3rd Red envelope: 0.25 Yuan, the balance: 8.14 yuan

The 4th Red Envelope: 0.98 Yuan, the balance: 7.16 yuan

The 5th Red envelope: 1.88 yuan, the balance: 5.28 yuan

The 6th Red envelope: 1.92 Yuan, the balance: 3.36 yuan

The 7th Red envelope: 2.98 Yuan, the balance: 0.38 yuan

The 8th Red envelope: 0.38 Yuan, the balance: 0 yuan

Summary:

Small set think this can be completely understood as a red envelope caused by the murders, small set only a few, and some engineering students directly thrown out the mathematical model, discrete functions, etc., but whether the algorithm is simple or complex, play happy enough.

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.