I need to generate pseudo-random codes for people to use to access my site. I wrote some code to allow this, but didn't realize that I was using character arrays and NOT integers as the seed (to make mt_rand repeatable). My demo code is here:
$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;
The result on 1and1 is:
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
But everywhere else, the result (localhost, other servers, etc.) is:
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
The problem is that on my present server (1and1) the system works fine (using the unpredictable character inputs). But I'm migrating to a new server and, of course, the codes look like the first set ALWAYS. In other words, I do not know how to generate the second set of random numbers (seeded with character arrays).
Can anyone tell me how to do this?
I want:
mt_srand('6565866669');
echo mt_rand(0, 10);
to return
97138956767
Struggling. Thank you!!
(Credit to therefromhere has pinpointed the issue: you have a 32 bit and a 64 bit server. Acccept his answer if he posts it)
This is your issue:
Solutions for handling big_ints are to use GMP or BCMath libraries, but these won't work in this case at the mt_srand() takes an integer - so you're limited to numbers between -2.1 billion and +2.1 billion, or installing 64 bit OS/Apache/PHP on your web server.