Jump to content

mt_rand and rand always returns lowest possible number


jordanwb

Recommended Posts

If I call mt_rand like so:

 

print mt_rand (0, 10);

 

It returns 0 and always 0. If I call mt_rand like so:

 

print mt_rand (1, 10);

 

It returns 1 and always 1. I'm running Gentoo with all the latest available packages. The same goes for the rand function.

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

 

So there's no need, but I'll try it anyways.

 

It sortof worked.

 

<?php

mt_srand (microtime ());
for ($i = 0; $i < 100; $i++)
{
print mt_rand (0, 9000)."<br />";
}

?>

 

It kept printing the same list each time I refreshed.

i had this problem on my local server, and never did quite figure out why it was happening.  try uploading the script to a server somewhere and look again - i'm willing to bet you'll find it works okay on an external server.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.