Click to See Complete Forum and Search --> : rand() Not Working on New Site (PHP 4.0.0)


msmith29063
07-21-2008, 12:09 PM
I just published a new small site to the live server. It's running PHP 4.0.0. And the rand() function is not working. I'm using it to pull up a random include file:
<?php require('includes/featured' . rand(1,4) . '.php'); ?>
I've used this code a thousand time but have never had trouble. Any ideas? Thanks!

NogDog
07-21-2008, 01:36 PM
You don't define "not working", but perhaps the problem is that prior to PHP 4.2.0, you need to seed the random number generator with srand (http://www.php.net/srand)() before calling rand().

Of course, the real problem is that there is a host anywhere still running PHP 4.0.0, which was released over 8 years ago. Besides, PHP4 has been officially dead since the start of this year, and not even security patches will be released after next month. (Of course, any host running 4.0.0 apparently is not very concerned with security patches, since the latest version 4.4.8. :rolleyes: ) Try to get the site migrated to a current PHP5 host ASAP (hopefully before PHP6 is released).

legendx
07-21-2008, 01:39 PM
"The trouble with random number generators is you never really know whether or not they're working"

-Dilbert

msmith29063
07-21-2008, 02:08 PM
I'm going to try to have the server upgraded -- but am not counting on it. This company offers hosting as a bundle with voice service.

BTW, the rand(1,4) function keeps returning 1.

NogDog
07-21-2008, 02:25 PM
I'm going to try to have the server upgraded -- but am not counting on it. This company offers hosting as a bundle with voice service.

BTW, the rand(1,4) function keeps returning 1.
That would be the expected result of not doing the srand(): always getting the same sequence of rand() results. Just add once call to srand() somewhere in the script before the first call to rand(). A typical usage is to use the timestamp as the seed (we're not talking super-critical random number requirements here, so nothing more complicated should be needed.

srand(time());
$rnd = rand(1,4);

msmith29063
07-21-2008, 03:24 PM
I see. Thank you for the information. Works now, obviously.