Edit report at https://bugs.php.net/bug.php?id=55519&edit=1

 ID:                 55519
 Updated by:         ahar...@php.net
 Reported by:        sergio dot bobillier at gmail dot com
 Summary:            mt_rand only generetes even 64 bits numbers
-Status:             Open
+Status:             Assigned
 Type:               Bug
 Package:            Math related
 Operating System:   Linux (Ubuntu 11.04 x64)
 PHP Version:        Irrelevant
-Assigned To:        
+Assigned To:        aharvey
 Block user comment: N
 Private report:     N

 New Comment:

Verified on 64-bit OS X, at least beyond a certain set of mt_rand() bounds. Not 
entirely sure what those actual bounds are yet.


Previous Comments:
------------------------------------------------------------------------
[2011-08-27 09:09:30] sergio dot bobillier at gmail dot com

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 this bug report at https://bugs.php.net/bug.php?id=55519&edit=1

Reply via email to