Heim >Backend-Entwicklung >PHP-Tutorial >生成不重复随机数的php 表达
生成不重复随机数的php 表述。
<?php function get_randoms($min,$max,$num){ $count = 0; $res = array(); while($count<$num){ $res[] = mt_random($min,$max); $res = array_flip(array_flip($res)); $count = count($res); } return $res; } $result = get_randoms(1,60,10); ?>
<?php function get_randoms($min,$max,$num){ $count = 0; $res = array(); while($count<$num){ $key = mt_random($min,$max); $res[$key] = $key; $count = count($res); } return $res; } $result = get_randoms(1,60,10); ?>