From:             
Operating system: Windows
PHP version:      5.3.8
Package:          Math related
Bug Type:         Bug
Bug description:mt_rand() poor quality with large magnitude negative $min

Description:
------------
Example output from the following script that generates 16 random numbers
in the 
range from -1073741823 to 2147483647. It shows that the number 1073741825
was 
repeated 4 times, or 25% of the time. This output is typical and the
repeated 
number is always 1073741825. The problem is less the smaller the magnitude
of 
$min.


Test script:
---------------
<?php

printf("PHP_VERSION=%s\n",PHP_VERSION);
$min = -(mt_getrandmax()>>1);
$max = mt_getrandmax();
$count = isset($argv[1])?(int)$argv[1]:16;
printf("min=%d, max=%d, count=%d\n",$min,$max,$count);

$repeat_counts = array();

for ($i=0;$i<$count;$i++) {
  $random = mt_rand($min,$max);
  if (!isset($repeat_counts[$random])) {
    $repeat_counts[$random] = 0;
  }
  $repeat_counts[$random]++;
  //printf("%12d%s\n", abs($random),($random<0)?"-":"");
}

$max_repeat_count = max($repeat_counts);
$same_value_max = array();
if ($max_repeat_count > 1) {
  foreach ($repeat_counts as $value => $repeat_count) {
    if ($repeat_count >= $max_repeat_count) {
      $same_value_max[] = $value;
    }
  }
  printf("The following number/s was/were repeated %d times (%s%%): %s\n"
    ,$max_repeat_count
    ,number_format(($max_repeat_count/$count)*100.0,1)
    ,implode(", ",$same_value_max)
    );
}


Expected result:
----------------
PHP_VERSION=5.3.8
min=-1073741823, max=2147483647, count=16



Actual result:
--------------
PHP_VERSION=5.3.8
min=-1073741823, max=2147483647, count=16
The following number/s was/were repeated 4 times (25.0%): 1073741825


-- 
Edit bug report at https://bugs.php.net/bug.php?id=60543&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=60543&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=60543&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=60543&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=60543&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=60543&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=60543&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=60543&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=60543&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=60543&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=60543&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=60543&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=60543&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=60543&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=60543&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=60543&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60543&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=60543&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=60543&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=60543&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=60543&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=60543&r=mysqlcfg

Reply via email to