PDA

View Full Version : mt_rand returning negative number



arne2
12-21-2006, 11:11 AM
Hi
How come my mt_rand returns negative numbers while it shouldn't?


<?
//SMALL MT_RAND COMPATIBILITY TEST
$echoout=mt_rand(1000000000000,22000000000000);
echo"$echoout";
?>

Sometimes it gives me a positive number (i once had : 75587147 ) but mostly i get things like -216672647
I think it's because the numbers are so big. Is there a function that creates a random number between two bcmathic numbers. I already use bcmathic functions like bcmul and bcmod etc on my page because of the high numbers. Is there a function that can create a random number between to big numbers?

THank you very much
P.S. It shouldn't be a very 'difficult' function, as simple as possible as i will have to execute it a lot of times, so it shouldn't take too long to calculate!

THANK YOU VERY MUCH FOR YOUR HELP!

arne2
12-21-2006, 03:35 PM
any help would be appreciated

firepages
12-21-2006, 03:35 PM
your numbers are too big,run...


<?
echo mt_getrandmax();
?>

to find out the larget possible random value you can get (will probably be 2147483647)

firepages
12-21-2006, 03:40 PM
also check out gmp_random() (http://www.php.net/gmp_random)

arne2
12-21-2006, 04:10 PM
yes my numbers are too big, but what can i do about it?
Is there something else than gmp_random because it works with bits and it's not what i'm looking for!

arne2
12-23-2006, 01:21 PM
anyone?

marek_mar
12-23-2006, 02:06 PM
A simple example:


function arbitrary_rand($min, $max)
{
return gmp_add($min, gmp_mod(gmp_random(), $max));
}

arne2
12-23-2006, 05:38 PM
thank you for your help but i'm having a problem : i think the GMP libary is not activated/included on the hosting server i'm on as the php page that uses it tells me : 'undefined function gmp_add'

Is there another solution? Thanks anyway!

arne2
01-06-2007, 07:40 PM
is there a way I don't need the GMP library installed to get it to work?
Is there another function that can help me out here, please?