Use PHP to send red envelopes. When we enter the number of red envelopes and the total amount, PHP will randomly allocate each amount based on these two values to ensure that everyone can receive a red envelope. The amount of each red envelope is different, which means that the amount of the red envelope must be different, and the total amount of all red envelopes should be equal to the total amount.
Let’s analyze the rules first.
The total amount is set to 10 yuan, and N individuals will receive it randomly:
N=1 First
Then the amount of red envelope = X yuan;
N=2 Second
To ensure that the second red envelope can be sent normally, the amount of the first red envelope = a random number between 0.01 and 9.99.
The second red envelope = 10-the amount of the first red envelope;
N=3 Third
Red envelope 1=a random number between 0.01 and 9.99
A random number from red envelope 2=0.01 to (10-red envelope 1-0.01)
Red envelope 3=10-Red envelope 1-Red envelope 2
……
So we get a rule that when allocating the current red envelope amount, we first reserve the minimum amount required for remaining red and white, and then take a random number between 0.01 and the total amount and reserved amount, and the random number obtained is the amount allocated by the current red envelope.
In actual application, the program first allocates the amount of red envelopes, that is, when sending the red envelope, the number of red envelopes and the amount of each red envelope are allocated. Then when the user comes to grab the red envelope, we can randomly return a red envelope to the user.
Red envelope allocation code:
$total=20;//Total red envelope amount$num=10;// Divide into 10 red envelopes, supporting 10 people to collect them randomly$min=0.01;//Everyone 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 'Part'.$i.'Red envelope:'.$money.' Yuan, balance:'.$total.' Yuan '; } echo 'Part'.$num.'Red envelope:'.$total.' Yuan, balance: 0 Yuan';
Running the above code will output the following results:
The first red envelope, amount is 2.08 yuan, balance is 17.92 yuan
The second red envelope, amount is 1.81 yuan, balance is 16.11 yuan
The third red envelope, amount is 0.15 yuan, balance is 15.96 yuan
The fourth red envelope, amount is 1.61 yuan, balance is 14.35 yuan
The 5th red envelope, amount of 1.11 yuan, balance of 13.24 yuan
The 6th red envelope, amount of 1.51 yuan, balance of 11.73 yuan
The 7th red envelope, amount of 1.21 yuan, balance of 10.52 yuan
The 8th red envelope, amount is 2.58 yuan, balance is 7.94 yuan
The 9th red envelope, amount of 5.4 yuan, balance of 2.54 yuan
The 10th red envelope, amount of 2.54 yuan, balance of 0 yuan
Attach the full code:
html code
<script type="text/javascript" src="/js/jquery/1.7.2/"></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} button, .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> </head> <body> <div > <div class="demo"> <button>generate10A red envelope,lump sum20Yuan</button> </div> <div ></div> <div class="ad_76090"><script src="/js/ad_js/bd_76090.js" type="text/javascript"></script></div><br/> </div> <script> $(function(){ $("button").click(function(){ $.ajax({ type: 'POST', url: '', dataType: 'json', beforeSend: function(){ $("#result").html('Red envelopes are being allocated'); }, success: function(json){ if(==1){ var str = ''; var res = ; $.each(res,function(index,array){ str += '<p><span>'+array['i']+'</span>red envelope, amount <span>'+array['money']+'</span>yuan, balance <span>'+array['total']+'Yuan</span></p>'; }); $("#result").html(str); }else{ $("#result").html('Data error!'); } } }); }); }); </script>
php code
<?php header("Content-Type: text/html;charset=utf-8"); $total=20;//Total red envelope amount$num=10;// Divide into 10 red envelopes, supporting 10 people to collect them randomly$min=0.01;//Everyone 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; $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 about this article. I hope it will be helpful for everyone to proficiently apply PHP to complete the red envelope distribution program.