1

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?

Alon Dor
  • 111
  • 10
  • You shouldn't do this. Good random number generation is extremely complex. If you insist, though, see this question: http://stackoverflow.com/questions/9482290/workaround-for-suhosin-mt-srand-ignore-to-consistently-shuffle-an-array-in-php – elixenide Feb 27 '14 at 15:43
  • I've already seen this question. Both caching & writing a different random function, won't do in my case, since I can't tell in advance what will be every seed my application produces, and I need a random function that produces exactly the same results as mt_rand & mt_srand produce, for backwards compatibility. – Alon Dor Feb 27 '14 at 17:01
  • The question @EdCottrell mentioned does not appear to have the world's greatest PRNG in any case. – abligh Feb 27 '14 at 17:04

1 Answers1

0

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.

Ben
  • 136
  • 2