PDA

View Full Version : [PHP] rand() for large numbers


Nullifi3d
04-17-2006, 12:49 PM
I need to generate a random number (with specified min and max) for small and large numbers. I'm using rand(*, *) right now and it doesn't seem to work with numbers in the billions. They go into the negative. What can I use instead?

DrewM
04-17-2006, 01:45 PM
<a href="http://us3.php.net/manual/en/function.rand.php" target="_blank">http://us3.php.net/manual/en/function.rand.php</a>

Nullifi3d
04-17-2006, 02:28 PM
I have already checked the php.net rand function manual. The only thing mentioned that seems to be even remotely close to what I am achieving is the statement:
Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than RAND_MAX, or consider using mt_rand() instead.
This says to either do what I am already doing (specifying a min and max) or using mt_rand() instead. Which on my server mt_rand() has the same MAX_RAND as rand() (2147483647). mt_rand() is faster though, so I should use it anyways right?

So how do I generate a random number between two numbers in the tens of billions?

DrewM
04-17-2006, 02:39 PM
sorry but I can't help you any more I thought that would have helped

Nullifi3d
04-17-2006, 02:48 PM
It's ok. It's quite strange. The manual states that if you specify a min and max then you can generate a number larger then the RAND_MAX, but it doesn't work with trying to generate a number between 90% and 95% of a number in the tens of billions.
$armyinfo['damage'] = mt_rand( (.90 * $armyinfo['strike_action']), (.95 * $armyinfo['strike_action']) );