Share WeChat red packets and Lucky Draw code

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. The specific idea is to define the total amount of money and the number of people, and then the minimum value is 0.01 yuan by default, and then perform a loop, the security value is set to enable the Later users to get the money. Try it by yourself. If you need it, you can study it in depth.
Red Envelope code: 02
// Red envelope Algorithm
03
Header ("content-type: text/html; charset = UTF-8 ");
04
$ Sum = 10; // total price
05
$ Num = 8; // number of people
06
$ Min = 0.01; // minimum value
07
For ($ I = 1; $ I <$ num; $ I ++ ){
08
$ Row = ($ sum-($ num-$ I) * $ min)/($ num-$ I); // security value
09
$ Money = mt_rand ($ min * 100, $ row * 100)/100;
10
$ Sum-= $ money;
11
Echo 'quarter '. $ I.', get '. $ money.' yuan, and the remaining '. $ sum.' yuan
';
12
}
13
Echo 'nth. $ num. 'person, get'. $ sum. 'yuan, and the remaining'. $ sum. 'yuan ';
14
?>
Lottery code 02
// Lottery
03
Header ("content-type: text/html; charset = UTF-8 ");
04
Function get_rand ($ arr ){
05
$ Arr_sum = array_sum ($ arr );
06
$ Arr_rand = mt_rand (1, $ arr_sum );
07
Foreach ($ arr as $ key => $ arr_num ){
08
$ Arr_sum-= $ arr_num;
09
If ($ arr_rand> $ arr_sum ){
10
Return $ key;
11
}
12
}
13
}
14
$ P = array (
15
'0' => array ('id' => 1, 'info' => 'first prize ', 'V' => 1 ),
16
'1' => array ('id' => 2, 'info' => 'second prize ', 'V' => 5 ),
17
'2' => array ('id' => 3, 'info' => 'third prize ', 'V' => 10 ),
18
'3' => array ('id' => 4, 'info' => '4', 'V' => 34)
19
);
20
Foreach ($ p as $ key => $ value ){
21
$ Arr [$ value ['id'] = $ value ['V'];
22
}
23
$ Rid = get_rand ($ arr );
24
$ Res ['yes'] = $ p [$ rid-1] ['info'];
25
Unset ($ p [$ rid-1]);
26
Shuffle ($ p );
27
For ($ I = 0; $ I
28
$ Pr [] = $ p [$ I] ['info'];
29
}
30
$ Res ['no'] = $ pr;
31
Var_dump ($ res );
32
?>

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.