Jump to content

Make mt_random always positive


viion

Recommended Posts

shouldn't it always be positive?

the manual says "returns a pseudo-random value between 0 and mt_getrandmax()" and i doubt that mt_getrandmax() would return a negative value.

otherwise you could specify a range that would produce positive numbers.

 

Scott.

yeah i just tried what i said and it definitely didn't work...so heres something i put together that does...maybe not the most efficient but it works nonetheless...

 

$number = abs(mt_rand(100,999));
$ab = abs(mt_rand(100,999));
$bc = abs(mt_rand(100,999));
$cd = abs(mt_rand(100,999));

echo $number.$ab.$bc.$cd;

Are you all aware, that these numbers are above 32bit integer range?

im not very up with the play on how PHP handles large numbers but php.net/int suggest numbers outside the 32bit range would be changed to a float that has a 64bit range that supports up to around 14 decimal digits.

 

Scott.

yeah...i just thought about that. so then you would just do something like

$number = abs(mt_rand(100,999));
$ab = abs(mt_rand(100,999));
$bc = abs(mt_rand(100,999));
$cd = abs(mt_rand(100,999));

$randomstring = $number.$ab.$bc.$cd;

$randomnumber = int($random);

 

then you have a random positive 12-digit integer....done and done  :) haha

I've never had a problem with mt_rand().

 

If you want a random number between 1 and 6 (rolling a di)

$di=mt_rand(1,6);

 

Between 1 and 24 (random hour of the day)

$di=mt_rand(1,24);

 

There's no need to abs() the result as it always returns a positive integer if you only supply positive inputs.

yes but he wants a 12-digit number...a number higher than 32-bit apparently...so you would need to shove a bunch together then intval() it to make it a number(64-bit) again...if he wants to do math calcs with it...otherwise, he could just leave it as a string..and you're right about the abs value thing, it is unnecessary

Ah!

 

Anything above a 32bit value will result in very incorrect values as numbers start to "wrap around" when outside of the limits. There's probably a set of scripts somewhere for handling larger values - I've seen one with dates outside of the normal range so I can't see why there isn't one for integers.

Imagine you have an unsigned tinyint - this ranges from 0 to 255. If you add 1 to it you don't get 256 because a tinyint can't be 256 - its upper limit is 255. I think it goes back to 0 - wraps around.

 

Same goes for higher numbers when outside their limits.

If PHP encounters a number beyond the bounds of the integer  type' date=' it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. [/quote']

 

The only problem is, mt_rand will always return 2^31-1 when the range is above 2^31.

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.