Home  >  Article  >  php教程  >  php随机概率红包算法

php随机概率红包算法

PHP中文网
PHP中文网Original
2016-05-23 08:39:041477browse


 /**
     * @param $num
     * @param $total
     * @param int $range  差距是否大
     * @return array|int
     */
    private function randomSplit($num,$total,$range=1){

        if($num<2){
            return $total;
        }

        $p=array();

        $min=floor($total/$num-$range);
        $max=floor($total/$num+$range);
        for($i=0;$i<$num-1;$i++){
            $depart=mt_rand($min,$max);
            $p[]=$depart;
            $total-=$depart;
        }
        $p[]=$total;
        return $p;

    }


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