From:
Operating system: Linux (Ubuntu 11.04 x64)
PHP version: Irrelevant
Package: Math related
Bug Type: Bug
Bug description:mt_rand only generetes even 64 bits numbers
Description:
------------
I was doing a lame RSA implementation when I discovered that mt_rand only
generates even numbers when the number is a 64 bits integer.
The Test script shows the script I used to generate two 64 bits integers p
and q with mt_rand. I was doing primality test only with odd numbers since
even numbers are not prime but the script never do the primality test
because all the generated numbers are even.
I thought this might be an integer overflow problem but I'm doing this on a
64 bits system and the var_dump function shows that the variables are
actually integers like so:
int(2117698586505379840)
int(1690740540401778688)
int(3279295862706012160)
int(4526730875131396096)
int(3188107862534520832)
int(1914802535743356928)
int(4172596828035874816)
int(4559249790717657088)
int(1938449129751445504)
int(1859647742213619712)
Test script:
---------------
function is_prime($num)
{
// See if the number is prime
echo "Primality test";
return true;
}
function gen_keys()
{
// 1. find two 64 bits primes p and q
$p = 0;
$q = 0;
while(!$p)
{
$p = mt_rand(72057594037927936, 4611686018427387904);
// debugging:
var_dump($p);
if($p % 2 == 0)
{
$p = 0;
}
else
{
// Primality test in another function
// (not important here)
echo "odd";
if(!is_prime($p))
$p = 0;
}
}
echo $p;
}
Expected result:
----------------
I expect the mt_rand to generate some odd numbers and make the script go
through the primality test or at least print "odd"
Actual result:
--------------
The script doesn't reach the primality or print "odd" test because all
generated numbers are even.
--
Edit bug report at https://bugs.php.net/bug.php?id=55519&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=55519&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=55519&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=55519&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=55519&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=55519&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=55519&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=55519&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=55519&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=55519&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=55519&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=55519&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=55519&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=55519&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=55519&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=55519&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=55519&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=55519&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=55519&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=55519&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=55519&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=55519&r=mysqlcfg