Skip to main content

reproducible random number series


How can i get a series of reproducible pseudorandom numbers in PHP?



In older versions of PHP i could do that just by using the same seed in the RNG , but it does not work anymore since PHP has changed the way rand and mt_rand works.



Please also see this comment in PHP.net page:




Keep in mind that the Suhosin patch which is installed by default on many PHP-installs such as Debian and DirectAdmin completely disables the srand and mt_srand functions for encryption security reasons. To generate reproducible random numbers from a fixed seed on a Suhosin-hardened server you will need to include your own pseudorandom generator code.




link of that comment: http://www.php.net/manual/en/function.srand.php#102636



Is there any solution ready? I do not have the time nor the experience to create my own pseudo-random generator code.



My goal is to have a code




<?php
//( pseudo random code here...)
$the_seed = 123; // 123 is just a number for demo purposes, NOT a static number
//...i hope you get the idea. It's just a hardcoded seed,
// it could be a seed based on a user-id, a date etc...
// we need the same output for a given seed.
//( pseudo random code here...)

// ...and finally
echo $the_random_number;
?>



so everytime i visit this page i should get the same number.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. One of the best random number algorithms by some metrics is Mersenne Twister.
    You can find a pure PHP version here (there are others).

    You can then call:

    init_with_integer($integer_seed)


    and get the same output (for a given seed) every time.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?