#50825 [NEW]: rand () and mt_rand () do not properly calculate when min=0

From: Date: Fri, 22 Jan 2010 16:42:56 +0000
Subject: #50825 [NEW]: rand () and mt_rand () do not properly calculate when min=0
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-146875@lists.php.net to get a copy of this message
From: xeriar at gmail dot com Operating system: Debian Squeeze PHP version: 5.2.12 PHP Bug Type: Math related Bug description: rand () and mt_rand () do not properly calculate when min=0 Description: ------------ Whenever rand () or mt_rand () are passed with a min=0, 0 gets reported less often than it should be. mt_rand (0,1) returns 1 75% of the time, for example, while mt_rand (1,2) works as expected. Reproduce code: --------------- <?php $arr = array (); for ($i = 0; $i < 100000; $i++) { $c = mt_rand (0,1); if (!isset ($arr[$c])) $arr[$c] = 1; else $arr[$c]++; } ksort ($arr); foreach ($arr as $key => $value) echo '<b>'.$key.':</b> '.$value.'<br />'; Expected result: ---------------- 0 and 1 should each get ~50,000 results Actual result: -------------- 0 gets ~25k results and 1 gets ~75k -- Edit bug report at http://bugs.php.net/?id=50825&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50825&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50825&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50825&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=50825&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50825&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=50825&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=50825&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=50825&r=needscript Try newer version: http://bugs.php.net/fix.php?id=50825&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=50825&r=support Expected behavior: http://bugs.php.net/fix.php?id=50825&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=50825&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=50825&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=50825&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50825&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=50825&r=dst IIS Stability: http://bugs.php.net/fix.php?id=50825&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=50825&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=50825&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=50825&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=50825&r=mysqlcfg

« previous php.bugs (#146875) next »