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