时间:2021-07-01 10:21:17 帮助过:25人阅读
function unique_random(& $result, $size=6, $min=0, $max=500){
$num = mt_rand($min, $max);
!in_array($num, $result) && array_push($result, $num);
return count($result) >= $size ? $result : unique_random($result, $size, $min, $max);
}
$result = array();
unique_random($result);
print_r($result);
Array
(
[0] => 132
[1] => 351
[2] => 43
[3] => 425
[4] => 449
[5] => 269
)
$arr = array();
for($i=1;$i<=500; $i++){
array_push($arr, $i);
}
shuffle($arr);
$ret = array_slice($arr, 0, 6);
print_r($ret);