最近在的一个项目,需要用到随机数,前提是生成的这些随机数不能有重复的,接下来吾爱编程为大家介绍一下关于PHP生成不重复随机数的方法,有需要的小伙伴可以参考一下:
1、项目需求:
随机生成一定数量的随机数,在执行指定方法的时候需要用到这些数据混合到指定的字符串中。
2、随机数代码:<?php
/**
* 生成一定数量的不重复随机数
* @param string $repo 随机数池
* @param int $num 指定生成数量
*/
function getDiyRand($repo, $length, $num)
{
$count = 0;
$return = array();
$repo_size = strlen($repo);
while ($count
$tmp_item = '';
$tmp_length = $length;
while ($tmp_length--) {
$tmp_index = mt_rand(1, $repo_size);
$tmp_item .= $repo[$tmp_index-1];
}
$return[] = $tmp_item;
$tmp_item = '';
$return = array_flip(array_flip($return));
$count = count($return);
}
shuffle($return);
return $return;
}
$repo = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rands = getDiyRand($repo, 12, 10);
var_dump($rands);
以上就是吾爱编程为大家介绍的关于PHP生成不重复随机数的方法,了解更多相关文章请关注吾爱编程网!