<?php
class
CBonus
{
public
$bonus
;
public
$bonus_num
;
public
$bonus_money
;
public
$money_single_max
;
public
function
construct(){
$this
->bonus_num = 10;
$this
->bonus_money = 200;
$this
->money_single_max = 60;
}
private
function
randomFloat(
$min
= 0,
$max
= 1) {
$mt_rand
= mt_rand();
$mt_getrandmax
= mt_getrandmax();
echo
'mt_rand=' .
$mt_rand
. ', mt_getrandmax=' .
$mt_getrandmax
. '<hr/>';
return
$min
+
$mt_rand
/
$mt_getrandmax
* (
$max
-
$min
);
}
public
function
compute()
{
$this
->bonus =
array
();
$bonus_money_temp
=
$this
->bonus_money;
$money_single_max
=
$this
->money_single_max;
$i
= 1;
while
(
$i
<
$this
->bonus_num)
{
if
(
$money_single_max
>
$bonus_money_temp
)
{
$money_single_max
=
floatval
(sprintf(
"%01.2f"
,
$bonus_money_temp
/ 2));
}
$bonus_money_rad
=
$this
->randomFloat(0.01,
$money_single_max
);
$bonus_money_rad
=
floatval
(sprintf(
"%01.2f"
,
$bonus_money_rad
));
$bonus_money_temp
=
$bonus_money_temp
-
$bonus_money_rad
;
$bonus_money_temp
=
floatval
(sprintf(
"%01.2f"
,
$bonus_money_temp
));
$this
->bonus[] =
$bonus_money_rad
;
$i
++;
}
$this
->bonus[] =
$bonus_money_temp
;
}
public
function
output(){
$total
= 0;
foreach
(
$this
->bonus
as
$k
=>
$v
)
{
echo
'红包' . (
$k
+1) . '=' .
$v
. '<br/>';
$total
+=
$v
;
}
echo
'红包总金额:'.
$total
;
}
}
$CBonus
=
new
CBonus();
$CBonus
->compute();
$CBonus
->output();
?>