2 回答
TA贡献1982条经验 获得超2个赞
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function getRandParcent(){
return rand(1,10)/rand(10,100);
}
function randUserMoney($cash,$min=6,$max=12){
$cash_ini = $cash;
$user_arr = array($min,$min,$min,$min,$min,$min,$min,$min,$min,$min);
$start = microtime_float();
while($cash>0){
$user_id = rand(0, 9);
$rand_point = getRandParcent();
if($user_arr[$user_id]<$max){
$ing = microtime_float();
if($ing-$start>0.01){
return randUserMoney($cash_ini);
}
$rand_money = round($rand_point*$cash,2);
$user_money = $user_arr[$user_id]+$rand_money ;
if($user_money<$max){
$user_arr[$user_id] = $user_money;
$cash = $cash - $rand_money;
}
}
}
return [
'user_money'=>$user_arr,
'total_money'=>array_sum($user_arr),
'excute_time'=>$ing-$start
];
}
var_dump(randUserMoney(40));
array (size=3)
'user_money' =>
array (size=10)
0 => float 11.59
1 => float 9.07
2 => float 11.99
3 => float 12
4 => float 9.14
5 => float 11.6
6 => float 11.86
7 => float 9.93
8 => float 6
9 => float 6.82
'total_money' => float 100
'excute_time' => float 0.004000186920166
TA贡献1803条经验 获得超6个赞
$cash = 40;
$user_arr = array(6,6,6,6,6,6,6,6,6,6);
while($cash>0){
$user_id = rand(0, 9);
if($user_arr[$user_id]<12){
$user_arr[$user_id]++;
$cash--;
}
}
;
var_dump($user_arr,array_sum($user_arr));die;
性能篇
$arr1=range(2,6);
shuffle($arr1);
$arr2=range(2,6);
shuffle($arr2);
$user_arr = array(6,6,6,6,6,6,6,6,6,6);
for ($i=0;$i<10;$i++){
if($i<=4){
$user_arr[$i] += $arr1[$i];
}else{
$j = $i%5;
$user_arr[$i] += $arr2[$j];
}
}
var_dump($user_arr,array_sum($user_arr));die;
添加回答
举报