Use PHP to write a red envelope program _php instance

Source: Internet
Author: User
Tags json php code

Use PHP to send red envelopes, when we enter the number of red envelopes and the total amount, PHP will be based on these two values to randomly allocate each amount, to ensure that everyone can receive a red envelope, each red envelope amount of money is required to have a difference in the amount of red envelopes should be equal to the total amount

Let's analyze the rules first.

Set the total amount of 10 yuan, there are n individuals to collect randomly:
N=1 First
The red envelope amount =x yuan;
N=2 a second
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 third.
A random number between red envelopes 1=0.01 to 9.99
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 we get a rule, in allocating the current red envelope amount, first reserve the remaining red and white required the minimum amount, and then in 0.01 to the total amount-reserve amount between the random number, the resulting random number is the current amount of red envelopes allocated.

In practical application, the program will first allocate the amount of red envelopes, that is, when the red envelopes, the number of red envelopes and the amount of each red packets are allocated, then the user to rob red envelopes, we randomly return a red envelope to the user.

Red Envelope Allocation Code:

 
$total =20;//Red Envelope Total amount  
$num =10;//is divided into 10 red envelopes, supports 10 people  
to receive at random $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);//The 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. ' Yuan ';  
}  
Echo ' $num. ' A red envelope: '. $total. ' Yuan, balance: 0 Yuan ';  

Running the above code will output the following results:

The 1th red envelope, the amount is 2.08 yuan, the balance 17.92 yuan
The 2nd red envelope, the amount is 1.81 yuan, the balance 16.11 yuan
The 3rd red envelope, the amount is 0.15 yuan, the balance 15.96 yuan
The 4th red envelope, the amount is 1.61 yuan, the balance 14.35 yuan
The 5th Red envelope, the amount is 1.11 yuan, the balance 13.24 yuan
The 6th red envelope, the amount is 1.51 yuan, the balance 11.73 yuan
The 7th Red envelope, the amount is 1.21 yuan, the balance 10.52 yuan
The 8th red envelope, the amount is 2.58 yuan, the balance 7.94 yuan
The 9th red envelope, the amount is 5.4 yuan, the balance 2.54 yuan
The 10th red envelope, the amount is 2.54 yuan, the balance 0 yuan

Attached complete code:

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 screens and (min-width:420px) {. demo{width:500p X 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>  

PHP code

<?php
Header ("Content-type:text/html;charset=utf-8");

$total =20;//The total amount of red envelopes 
$num =10;//into 10 red envelopes, supporting 10 people to receive a random 
$min =0.01;//at least 0.01 for each person 

($i =1; $i < $num; $i + +) { 
  $safe _total= ($total-($num-$i) * $min)/($num-$i);//The Random security upper bound 
  $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 mentioned is the entire content of this article, I hope to be proficient in the use of PHP to send a red envelope program to help.

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.