我生成从以下字符的6位数code。这些将被用于对贴邮票。 他们将在10k或更小分批产生(打印前),我不预期有可能永远也无法超过1-2万总(可能要少得多)。 之后,我产生了codeS批次,我会检查现有的codeS的MySQL数据库,以确保没有重复。
//排除问题的字符:B8G6I1l0OQDS5Z2
$字符='ACEFHJKMNPRTUVWXY4937';
$字符串='';
为($ i = 0; $ I< 6,$ I ++){
。$字符串= $字[兰特(0,strlen的($字符) - 1)]。
}
返回$串;
这是一个坚实的方式来产生code? 有多少可能的排列会不会有? (6位$ C $从21个字符池c)。对不起数学不是我的强项 解决方案
21的6次方= 85766121的可能性。
使用数据库和存储使用的值是坏的。如果你想假随机性,你可以使用以下命令:
减少到19可能的数字,并利用这样的事实阶数p ^ k的基团,其中p是一个奇素数总是环状的。
取顺序7 ^ 19组,使用一台发电机共贷到7 ^ 19(我来接13 ^ 11,你可以选择任何不被7整除)。
然后执行以下工作:
$previous = 0;
函数发生器($previous)
{
$发生器= POW(13,11);
$模数= POW(7,19); // INT可能太小
$ possibleChars =ACEFHJKMNPRTUVWXY49;
$previous =($previous + $发生器)%$模量;
$输出='';
$ TEMP = $previous;
为($ i = 0; $ I< 6,$ I ++){
$输出+ = $ possibleChars [$ temp%19]。
$ TEMP = $温度/ 19;
}
返回$输出;
}
它会遍历所有可能的值,并显得有点乱,除非他们去挖掘。一个更安全的替代办法是乘组,但我忘了我的数学早已:(
I'm generating a 6 digit code from the following characters. These will be used to stamp on stickers. They will be generated in batches of 10k or less (before printing) and I don't envisage there will ever be more than 1-2 million total (probably much less). After I generate the batches of codes, I'll check the MySQL database of existing codes to ensure there are no duplicates.
// exclude problem chars: B8G6I1l0OQDS5Z2
$characters = 'ACEFHJKMNPRTUVWXY4937';
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
Is this a solid approach to generating the code? How many possible permutations would there be? (6 Digit code from pool of 21 characters). Sorry math isn't my strong point
解决方案
21^6 = 85766121 possibilities.
Using a DB and storing used values is bad. If you want to fake randomness you can use the following:
Reduce to 19 possible numbers and make use of the fact that groups of order p^k where p is an odd prime are always cyclic.
Take the group of order 7^19, using a generator co-prime to 7^19 (I'll pick 13^11, you can choose anything not divisible by 7).
Then the following works:
$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;
}
It will cycle through all possible values and look a little random unless they go digging. An even safer alternative would be multiplicative groups but I forget my math already :(