Whim to the Alumni Micro-letter group of red Envelopes, I set the total amount of red envelopes for 10 yuan, support 28 people randomly pick up.
So an interesting result came up:
A has received 0.26 dollars
B received 0.29 dollars.
C has received 0.02 yuan
D has received 0.56 yuan
E has received 0.64 dollars
......
What kind of algorithm does the micro-letter use? Simple Baidu, there is no official explanation, just in the know there is a more popular discussion, links poke here, but they discussed too deeply, there is a suspicion of the pit.
I tried it according to my own logic, this algorithm needs to meet the following requirements:
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
......
So far, the law appears! Start coding!
PHP code
Header ("Content-type:text/html;charset=utf-8");/output not garbled, you know
$total =10;//red envelope total
$num =8;//divided into 8 red envelopes, support 8 people randomly pick up
$min =0.01;//Each person receives at least 0.01 yuan for
($i =1; $i < $num $i + +)
{
$safe _total= $total-($num-$i) * $MIN; Random security upper bound
$money =mt_rand ($min *100, $safe _total*100)/100;
$total = $total-$money;
Echo ' $i. ' A red envelope: '. $money. ' Yuan, balance: '. $total. ' <br/> ';
}
Echo ' $num. ' A red envelope: '. $total. ' Yuan, balance: 0 Yuan ';
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
PHP code
Header ("Content-type:text/html;charset=utf-8");/output not garbled, you know
$total =10;//red envelope total
$num =8;//divided into 8 red envelopes, support 8 people randomly pick up
$min =0.01;//Each person receives at least 0.01 yuan for
($i =1; $i < $num $i + +)
{
$safe _total= ($total-($num-$i) * $min)/($ num-$i);//Random Safety cap
$money =mt_rand ($min *100, $safe _total*100)/100;
$total = $total-$money;
Echo ' $i. ' A red envelope: '. $money. ' Yuan, balance: '. $total. ' <br/> ';
}
Echo ' $num. ' A red envelope: '. $total. ' Yuan, balance: 0 Yuan ';
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
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.