我需要生成伪随机代码供人们用来访问我的网站。我编写了一些代码来实现这一点,但没有意识到我使用字符数组而不是整数作为种子(使 mt_rand 可重复)。我的演示代码在这里:
$seedVal = array(6565866669, 6565866670, 6565866671);
foreach ($seedVal as $seed) {
mt_srand($seed);
$rnd = '';
for ($i = 0; $i < 11; $i++) { // Loop over the string length
$tmp = mt_rand(0, 10);
$rnd .= $tmp;
}
echo "Seed = $seed, RandNum = $rnd </br>";
}
echo "</br>Now with character seeds</br>";
$seedVal = array('6565866669', '6565866670', '6565866671');
foreach ($seedVal as $seed) {
mt_srand($seed);
$rnd = '';
for ($i = 0; $i < 11; $i++) { // Loop over the string length
$tmp = mt_rand(0, 10);
$rnd .= $tmp;
}
echo "Seed = $seed, RandNum = $rnd </br>";
}
return;
1and1 的结果是:
Seed = 6565866669, RandNum = 425453109319
Seed = 6565866670, RandNum = 39388593858
Seed = 6565866671, RandNum = 891273965110
Now with character seeds
Seed = 6565866669, RandNum = 97138956767
Seed = 6565866670, RandNum = 97138956767
Seed = 6565866671, RandNum = 97138956767
但在其他任何地方,结果(本地主机、其他服务器等)是:
Seed = 6565866669, RandNum = 425453109319
Seed = 6565866670, RandNum = 39388593858
Seed = 6565866671, RandNum = 891273965110
Now with character seeds
Seed = 6565866669, RandNum = 425453109319
Seed = 6565866670, RandNum = 39388593858
Seed = 6565866671, RandNum = 891273965110
问题是在我目前的服务器(1and1)上,系统工作正常(使用不可预测的字符输入)。但是我正在迁移到新服务器,当然,代码看起来总是像第一组一样。换句话说,我不知道如何生成第二组随机数(以字符数组为种子)。
谁能告诉我该怎么做?
我想:
mt_srand('6565866669');
echo mt_rand(0, 10);
返回
97138956767
挣扎。谢谢!!