php红包

<?php
header('Content-Type:text/html;charset=utf-8');

$total  = 10;//金额
$num    = 6;//红包数
$min    = 0.01;//最小金额
  
$total  = $total*100;
$arr    = array();
for($i=1; $i<$num; $i++){
    $a = mt_rand(min*100,$total);
    $b = $total - $a;
   // 每次用所有金额最大的拆分
    if($a != 0 && $b != 0){
        array_push($arr,$a,$b);
        sort($arr);
        if($i <$num -1){
            $total = array_pop($arr);
        }
        if($i == $num -1){
            shuffle($arr);
        }
    }
}
for($i =1; $i<=$num; $i++){
    echo '第'.$i.'个红包'.($arr[$i-1]/100).'元<br/>'; 
}

  

猜你喜欢

转载自www.cnblogs.com/lnmy/p/9168039.html