Srand() issue

Does anybody have any idea why the result of following code is different each time:

<?php

srand(152);
echo rand();
echo '<br>';

srand(152);
echo rand();
echo '<br>';

srand(152);
echo rand();
echo '<br>';

srand(152);
echo rand();

?>


1515441909
654160073
1204705132
711690770

213509977
902459421
1801474686
900418819

.... 

The manual on srand suggests (http://php.net/manual/en/function.srand.php) that your PHP install might be running suhosin in which case srand doesn’t do anything.

If you want a random number generator that can be seeded have a look at this article: http://www.sitepoint.com/php-random-number-generator/

Maybe i missed something here… but…
“the result of following code is different each time”
… is this not the desired result of rand?

Yes, but srand seeds rand, gives it a fixed starting point so to speak, which is wanted for certain experiments that need to be repeatable so peers can check your results.

except that you cant predict the server load, which would slow your script and return differing values of rand…

so… i’m still lost. If you want repeatability, used a fixed number set (which can be Rand-constructed)…

You’re assuming here that rand() uses the current time every time it gives you a random number. It doesn’t. It starts off with a certain number, called the seed (which is indeed usually based on the current timestamp) and from there on it will just uses a formula with the previous number as input to generate the next number.

Consider this:


<?php

srand(2);
echo rand(), "\
";
echo rand(), "\
";

srand(2);
echo rand(), "\
";
sleep(2);
echo rand(), "\
";

This outputs the following, each and every time I run it (Kubuntu 32 bit 3.2.0-34-generic-pae):


1505335290
1738766719
1505335290
1738766719

So we can safely conclude that the time between rand()-calls doesn’t matter.

You could also do that, but saying “I used Mersenne_twister with a seed of 932382” is a lot easier than having to give someone else a list of numbers you used. Which may be thousands upon thousands of numbers, depending what you used them for.

guys, I’m in special case that I want to have fixed numbers by rand() function, in many servers I’ve got fixed numbers in following code:


php -r "srand(152); echo rand();"

Where output is always 979730751, like http://y-shahinzadeh.ir/srand.php

I’ve tested in many places, however, in last server that I wanted to deploy my code, it resulted in not having fixed numbers:



root@portal:~# php -r "srand(152); echo rand();"
46330306root@portal:~# php -r "srand(152); echo rand();"
1169457841root@portal:~# php -r "srand(152); echo rand();"
814871624root@portal:~# php -r "srand(152); echo rand();"
1096079710root@portal:~# php -r "srand(152); echo rand();"
323949937root@portal:~# php -r "srand(152); echo rand();"
1595318222root@portal:~# 



I know. I answered in post #2

Try to find out if the server does indeed have suhosin installed and if you access to php.ini try adding this to it:


suhosin.srand.ignore off

Thank in advance, I accomplished addressing this issue, it was due to SuHosin as ScallioXTX said.