1
<?php
 /**
 *    @param $count 随机数个数
 *    @param $sum 随机数总和
 *    @param $mix 最小随机数
 *    @param $max 最大随机数
 * 
 */
function getRand($count,$sum,$mix,$max){
        ini_set('memory_limit', "1024M");
        $i=1;
        while ($i <= $count) {
            
            $tmp[] = mt_rand($mix,$max);
            $i++;
        }
        $k = $sum / array_sum($tmp);
        $res = [];
        foreach ($tmp as $v) {
            $res[] =  (int) ($v*$k);
        }
        $assoc = $sum - array_sum($res);
        $last = array_pop($res);
        $res[] = $last + $assoc;
        while (max($res) > $max) {
            $tmax = max($res);
            $key = array_search($tmax, $res);
            if($key !== false){
                 $res[$key] = mt_rand($mix,$max);
                 $k = array_search( min($res), $res);
                 $res[$k] = $tmax - $res[$key] + min($res);
            }
        }
        return $res;
}
$res  = getRand(300,400000,300,2000);
var_dump(count($res));
var_dump($res[1]);
var_dump(array_sum($res));

missonce
96 声望5 粉丝

php 菜鸟