Lua vs PHP随机数[关闭]

I have a lua script that encrypts a string and sends it via http to a PHP script. Problem is, the encryption process uses Lua's math.randomseed and math.random. When trying to decrypt the string in PHP, mt_srand and mt_rand produce different numbers than Lua did.

How can I get php to generate numbers like Lua?

-- Edit

Ok, so in my (very simple) encryption I'm using a key to generate a seed. That seed lets me get back the same "random number" each time.

So if my key produces a seed of say, 80 and I use this in Lua...

math.randomseed(80)
local randomNumber = math.random(1, 20)
// randomNumber = 3

When trying to decrypt in PHP, I'll use the same seed but I get a different output.

mt_srand(80);
$randomNumber = mt_rand(1, 20);
// $randomNumber = 10

I need to figure out a way to get back the same number so that I'm able to decrypt the string.

You need a random-number-generator which use the same algorithm on php and lua.

Either you find an random-number-generator for Lua and one for PHP which has the same implementation, or you must programm your own one. Moreover, using of system intern generators are also bad, because they can change everytime, and then your script also no longer works.

Instead of trying to reinvent the wheel by replicating an existing random function from either Lua or PHP for the opposite language. I found that using the approach on the following link to be much easier. Simply ported the code to Lua and all is well. Obviously, it's not going to fit everyones case, but for how I'm using it, it's perfect and simple. http://www.sitepoint.com/php-random-number-generator/

class Random {

    // random seed
    private static $RSeed = 0;

    // set seed
    public static function seed($s = 0) {
        self::$RSeed = abs(intval($s)) % 9999999 + 1;
        self::num();
    }

    // generate random number
    public static function num($min = 0, $max = 9999999) {
        if (self::$RSeed == 0) self::seed(mt_rand());
        self::$RSeed = (self::$RSeed * 125) % 2796203;
        return self::$RSeed % ($max - $min + 1) + $min;
    }

}

Only thing I changed was the default seed, and removed the line using mt_rand()

Then in Lua I created the following code:

local mySeed = 0;

function setSeed(s)
    mySeed = math.abs(tonumber(s)) % 9999999 + 1;
    myRand();
end

function myRand(min, max)
    min = min or 0;
    max = max or 9999999;
    mySeed = (mySeed * 125) % 2796203;
    return mySeed % (max - min + 1) + min;
end