$value) { $rand = mt_rand(1, $probabilitySum); if ($rand <= $value) { $result = $key; }else{ $probabilitySum -= $value; } } return $result; } $data = array( 1 => 10, 2 => 30, 3 => 40, 4 => 90, 10 => 100 ); var_dump(probability($data));
本文共 423 字,大约阅读时间需要 1 分钟。
$value) { $rand = mt_rand(1, $probabilitySum); if ($rand <= $value) { $result = $key; }else{ $probabilitySum -= $value; } } return $result; } $data = array( 1 => 10, 2 => 30, 3 => 40, 4 => 90, 10 => 100 ); var_dump(probability($data));
转载于:https://www.cnblogs.com/yuwensong/p/6432262.html