Red envelopes are a popular feature, and red envelopes are used for both ages and ages. Next, let's share with you an algorithm commonly used in php to implement the red envelope program, I hope this program will be helpful to our friends who are working on sending red packets using PHP. after we enter the number and total amount of red packets, PHP will randomly allocate each amount based on these two values, make sure that everyone can receive a red envelope. the amount of each red envelope varies, that is, the amount of the red envelope must be different. The total amount of all red envelopes should be equal to the total amount.
View demo download source code
First, let's analyze the rule.
Set the total amount to 10 yuan, and N people will receive them at random:
N = 1 First
The amount of the red packet is X RMB;
N = 2 second
To ensure that the second red envelope can be normally issued, a random number between the first red envelope amount = 0.01 and 9.99.
The second red envelope = 10-the amount of the first red envelope;
N = 3 third
Red Packet 1 = a random number between 0.01 and 9.99
Red packet 2 = a random number between 0.01 and (10-red packet 1-0.01)
Red packet 3 = 10-red packet 1-red packet 2
......
Therefore, we get a rule that, when allocating the current amount of red packets, we reserve the minimum amount required for the remaining red and white, and then take a random number between 0.01 and the total amount-the reserved amount, the random number is the amount allocated to the current red packet.
In actual application, the program first allocates the red envelope amount, that is, when the red envelope is issued, the number of red envelopes and the amount of each red envelope are allocated, then when the user grabs the red envelope, we can return a random red envelope to the user.
Red envelope distribution code:
$ Total = 20; // total amount of red packets $ num = 10; // It is divided into 10 red packets and can be randomly received by 10 people. $ min = 0.01; // Each person can receive at least 0.01 yuan for ($ I = 1; $ I <$ num; $ I ++) {$ safe_total = ($ total-($ num-$ I) * $ min)/($ num-$ I ); // random security limit $ money = mt_rand ($ min * 100, $ safe_total * 100)/100; $ total = $ total-$ money; echo 'quarter '. $ I. 'red packets :'. $ money. 'dollar, balance :'. $ total. 'meta';} echo 'quarter '. $ num. 'red packets :'. $ total. 'Yuan, balance: 0 yuan ';
After you run the preceding code, the following results are output:
1st red packets, amounting to 2.08 yuan, with a balance of 17.92 yuan
2nd red packets, amounting to 1.81 yuan, with a balance of 16.11 yuan
3rd red packets, amounting to 0.15 yuan, with a balance of 15.96 yuan
4th red packets, amounting to 1.61 yuan, with a balance of 14.35 yuan
5th red packets, amounting to 1.11 yuan, with a balance of 13.24 yuan
6th red packets, amounting to 1.51 yuan, with a balance of 11.73 yuan
7th red packets, amounting to 1.21 yuan, with a balance of 10.52 yuan
8th red packets, amounting to 2.58 yuan, with a balance of 7.94 yuan
9th red packets, amounting to 5.4 yuan, with a balance of 2.54 yuan
10th red packets, amount 2.54 yuan, balance 0 yuan
The above is the use of php to implement the red packet program, hope to help everyone, there is a need for source code friends, you can download directly.