Home  >  Article  >  Backend Development  >  PHP implements lucky red envelope function

PHP implements lucky red envelope function

PHP中文网
PHP中文网Original
2017-09-01 16:53:112024browse

/**
 * php拼手气红包函数
 * @param int $fee 红包总金额
 * @param int $c 发给多少人
 * @return array
 */
function RedEnvelope($fee,$c){
    $fee = 100;
    $n   = $fee/2;
    for($i=0;$i<$c;$i++){
        if($i==($c-1)){
            $arr[]=$fee;
        }else{
            $arr[]=$j=rand(0.01,$n)+rand(11,99)/100;
            $fee = $fee-$j;
            $n = $fee/4;
        }
    }
    return $arr;
}

//使用示例
$arr=RedEnvelope(100,10);
print_r($arr);
$z =0;
foreach($arr as $v){
    $z +=$v;
    echo $v."+";
}
echo "=",$z;

PHP implements lucky red envelope function

The above is the detailed content of PHP implements lucky red envelope function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn