Problem with rand()<

windows

Guest
Ok, I've been using the rand() function for a while now without any problems, to choose an advertisement to display. Today while I was using the local version of my site I noticed that the ads were never changing.

Since my pages use quite a bit of php in more than one file, I decided to start with a new file containing only the following:
for ($x=0; $x<10000; $x++) {
echo rand().' ';
}

It output 10000 random numbers like it was supposed to, except that no matter how many times I refresh the page I always get the same numbers in the same order (all 10000 of them!)

If I put srand() at the beginning of the file it works, but I know I shouldn't need to use it, cause I'm using PHP 4.3.3 and the manual says its not neccesary for this version. Also I know that my code was working a couple days ago, and I've never used srand() before.

Anyone know what's going on here?strange - but if it works using srand() then just use it.

there's nothing with using it in new PHP builds - maybe the server was updated and something happened there....Originally posted by Horus_Kol
maybe the server was updated and something happened there....

No the server is working fine, this is my local version thats giving problems, the one on my pc. I didn't change my version of php, or my code, it just seems to have stopped working...

OK I found out whats causing it. I had copied the php.ini-dist file to php.ini and made a couple changes so I could enable the use of sessions. I never had a php.ini file before. Just now I renamed php.ini to something else and rand() is working properly again, but now I can't use sessions. Even if I just rename php.ini-dist to php.ini without changing any of the contents, it still messes up rand().

Is there anything related to rand() in the php.ini-dist file?not sure - possibly...you should seed the rand() first


srand ((double)microtime()*1000000);
$randval = rand(10,100000);
echo $randval;

no need to use a for loop. if it needs it or not I stil use itOriginally posted by scoutt
you should seed the rand() first

OK, but I still don't see why having a php.ini while would make it neccesary to use srand().you have to have one anyway. php needs it. it also should be in your windows folder.Well up until a couple days ago I didn't had a php.ini anywhere on my harddrive. I have a php.ini-dist and a php.ini-recommended. Php has been running fine for me for months without it.it shoudln't work without a php.ini in your windows folder.Horus is correct. you can't run it without a ini file. if you did have it running then you havn't done anything that required php. if so then you got a very basic default settings. I think they are loaded in the .exe file just for this purpose. tha tmeans you can't use superglobals, sessions, extensions, etc..
 
Back
Top