Forum Moderators: coopster

Message Too Old, No Replies

Predictable Random numbers

         

Alcoholico

11:31 pm on May 26, 2010 (gmt 0)

10+ Year Member



Hello.
I have a script running for years on a PHP4 server, this script depends on the mt_srand() and mt_rand() php functions. I am moving now this script to a PHP5 server and I am getting different non-predictable values on this server. The thing is I need both servers to output the same values, that's why I was using mt_srand() which allegedly does that, everything else is exactly the same, only the PHP versions are different. Would there be a way without using these functions to generate a predictable, seedable random value, or force php5 random functions to behave like php4?
Thank you.

jatar_k

1:08 pm on May 27, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this is the correct behaviour, is that php 4 server unpatched? The old versions of rand functions, if unseeded, would produce the same numbers, I thought they would function correctly when seeded as the seed was pseudo random but I could be wrong.

the point of random is to be unpredictable

Maybe if you explain what you want to achieve exactly someone may have a suggestion, are there some criteria that you have that could be met using a combination of other functions or maybe a logic change.

Readie

3:29 pm on May 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This thread [webmasterworld.com] may be useful to you here - The script there is designed to change every week, but you could very easily adjust it to suit your purposes.

Alcoholico

5:07 pm on May 27, 2010 (gmt 0)

10+ Year Member



Thanks jatar_k and Readie.
Yes, I know the nature of a random number, but not always it needs to be unpredictable, sometimes it is very useful to change a variable while the others remain the same, that's the point of a random seed, to be able to get the same random series repeatedly.
@Readie, I'd already read that thread but that solution relies on srand() which behaves pretty much like mt_srand(), ie, different values depending on php version.
What I need is a reliable way to generate a series of random numbers using a seed which outputs exactly the same values no matter the php version or machine I use.

Readie

1:56 pm on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a link to this post [webmasterworld.com] on php.net today, may be your solution.

Alcoholico

4:42 pm on May 28, 2010 (gmt 0)

10+ Year Member



Thanks a lot Readie though that solution didnt work for me, I tried
ini_set('suhosin.srand.ignore', 0); 

also in .htaccess: I tried
php_value suhosin.srand.ignore 0


No luck. I had big hopes but nothing changed, as a quick and very dirty fix I'll downgrade the new server to php4 until I find a way to get consistent results. One solution I'm considering now would be to create a big database of possible values generated on the old server for backwards compatibility or use the OS random generator with no backwards compatibility and no possibility of testing on a Windows desktop.