Use PHP to write a red envelope program, and php to write a red envelope Program
When you use PHP to send a red envelope, after entering the number and total amount of the red envelope, PHP will randomly allocate each amount based on the two values to ensure that everyone can receive a red envelope, each of which has a different amount, that is, the amount of red packets must be different. The total amount of all red packets should be equal to the total amount.
Let's first 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
Complete code is attached:
Html code
<Script type = "text/javascript" src = "http://libs.useso.com/js/jquery/1.7.2/jquery.min.js"> </script> <style>. demo {width: 300px; margin: 60px auto 10px auto} @ media only screen and (min-width: 420px ){. demo {width: 500px; margin: 60px auto 10px auto }}. demo p {height: 62px; line-height: 30px }. demo p label {width: 100px; text-align: right }. input {width: 140px; height: 24px; line-height: 14px; border: 1px solid # d3d3d3} B Utton ,. button {background-color: # f30; color: white; border: none; box-shadow: none; font-size: 17px; font-weight: 500; font-weight: 600; border-radius: 3px; padding: 15px 35px; margin: 26px 5px 0 0px; cursor: pointer;} button: hover ,. button: hover {background-color: # f00 ;}# result {width: 360px; margin: 10px auto} # result p {line-height: 30px} # result p span {margin: 4px; color: # f30} </style>
Php code
<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ total = 20; // total red packets $ num = 10; // The total number of red packets is 10, 10 people can receive $ min = 0.01 at random. // 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 upper limit $ money = mt_rand ($ min * 100, $ safe_total * 100)/100; $ total = $ total-$ money; $ arr ['res'] [$ I] = array ('I' => $ I, 'money' => $ money, 'Total' => $ total );} $ arr ['res'] [$ num] = array ('I' => $ num, 'money' => $ total, 'Total' => 0); $ arr ['msg '] = 1; echo json_encode ($ arr);?>
The above is all the content of this article. I hope it will be helpful for you to skillfully use php to complete the red packet program.