Qua*_*ad6 8 php random algorithm permutation
我正在从以下字符生成一个6位数的代码.这些将用于在贴纸上盖章.
它们将分批生成10k或更少(打印前),我不认为总计将超过1-2百万(可能更少).
生成批量代码后,我将检查现有代码的MySQL数据库,以确保没有重复.
// exclude problem chars: B8G6I1l0OQDS5Z2
$characters = 'ACEFHJKMNPRTUVWXY4937';
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
Run Code Online (Sandbox Code Playgroud)
Jea*_*rin 12
21 ^ 6 = 85766121种可能性.
使用DB并存储使用的值很糟糕.如果您想伪造随机性,可以使用以下内容:
减少到19个可能的数字,并利用p为奇数素数的p ^ k组总是循环的事实.
取7 ^ 19的顺序组,使用生成器co-prime到7 ^ 19(我将选择13 ^ 11,你可以选择任何不能被7整除的东西).
然后以下工作:
$previous = 0;
function generator($previous)
{
$generator = pow(13,11);
$modulus = pow(7,19); //int might be too small
$possibleChars = "ACEFHJKMNPRTUVWXY49";
$previous = ($previous + $generator) % $modulus;
$output='';
$temp = $previous;
for($i = 0; $i < 6; $i++) {
$output += $possibleChars[$temp % 19];
$temp = $temp / 19;
}
return $output;
}
Run Code Online (Sandbox Code Playgroud)
它会循环遍历所有可能的值,看起来有点随机,除非他们去挖掘.一个更安全的替代方案是乘法组,但我已经忘记了我的数学:(
rand看str_shuffle和随机性.rand到mt_randmemcached无论redis是否使用MySQL总可能性
21 ^ 6 = 85,766,121
Run Code Online (Sandbox Code Playgroud)
85,766,121 应该没问题,要将数据库添加到这一代,请尝试:
例
$prifix = "stamp.";
$cache = new Memcache();
$cache->addserver("127.0.0.1");
$stamp = myRand(6);
while($cache->get($prifix . $stamp)) {
$stamp = myRand(6);
}
echo $stamp;
Run Code Online (Sandbox Code Playgroud)
使用的功能
function myRand($no, $str = "", $chr = 'ACEFHJKMNPRTUVWXY4937') {
$length = strlen($chr);
while($no --) {
$str .= $chr{mt_rand(0, $length- 1)};
}
return $str;
}
Run Code Online (Sandbox Code Playgroud)