How to write my own mt_rand() & mt_srand?

256 views Asked by At

I have an application that makes a heavy use of mt_rand & mt_srand, the problem is that many host providers are enabling, by default, the suhosin.mt_srand.ignore, which cause my application not to work properly, because the seeding doesn't work...

Since my application is already running on some hosting servers (that do not enable this extension) I would want to create my own mt_rand & mt_srand functions, in php, and have them return the same results (for the same seeds) as the built-in functions, that way I will not depend on whether the host provider enabled this extension or not.

I've looked at rand.c (http://lxr.php.net/xref/PHP_5_4/ext/standard/rand.c), which is the source code for mt_rand() & mt_srand(), but my knowledge of C is poor, and I can't migrate it to php by myself.

Does anyone know how can it be done? or whiling to help me with that?

1

There are 1 answers

0
Ben On

You may try a PHP-only implementation of a common PRNG algorithm, e.g. https://github.com/ruafozy/php-mersenne-twister

However, please do not rely on these numbers to be cryptographically secure - meaning use these e.g. for generating a random maze in a game but not for encrypting your diary.