Question

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!!

Was it helpful?

Solution

(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:

  1. mt_srand() takes an integer paramter
  2. PHP will (helpfully) try to cast your string as an integer when required for a parameter
  3. The maximum integer on 64 bit systems is 4.something gazillion - so your string will convert to an integer, but on 32bit systems, the maximum integer is 2.1 billion, so your string will convert to a different integer withing the allowed range (it goes to 2.1 billion, then wraps to -2.1 billion and counts up and repeats).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top