Forum Moderators: coopster
<?php
// seed with microseconds
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
$randval = mt_rand();
?>
Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers that should be suitable for seeding some kinds of cryptography (see the home page for details) and is four times faster than what the average libc provides.
So, first off, you are probably better off using mt_rand(). You may also want to echo the mt_getrandmax [php.net] to return the maximum value that can be returned by a call to mt_rand() on your system just to make sure it isn't 1 ;)
I wonder if the 1 being returned every time indicates a numeric value or boolean
true...?
<?php
print 'mt_getrandmax is: ' . mt_getrandmax();
?>
There are a few quirks and bugs reported at that level of PHP (4.0.5):
Problem in rand() on win2000 (or all win32?) [bugs.php.net]
rand() without srand() doesn't work with certain php.ini [bugs.php.net]
I would recommend updating your PHP for one thing, not just for this rand issue, but there are many other reasons for keeping current with your PHP version levels, security being primary. I realize this may not solve your problem, but it's worth a shot instead of battling a possible bug.