php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60559 mt_rand() backward incompatibility
Submitted: 2011-12-19 10:31 UTC Modified: 2012-11-15 17:49 UTC
Votes:4
Avg. Score:2.8 ± 1.8
Reproduced:1 of 4 (25.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kartyl at gmail dot com Assigned: googleguy (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.8 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kartyl at gmail dot com
New email:
PHP Version: OS:

 

 [2011-12-19 10:31 UTC] kartyl at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/migration53.incompatible
---
It looks like in PHP 5.2 mt_rand($a, $b) returned the value from the range between $a and $b. There was no the requirement that $b must be greater than $a (checked on PHP 5.2.17).

In PHP 5.3.8 the warning is triggered and boolean false is returned if second parameter is less than the first.

[19-Dec-2011 11:26:47] PHP Warning:  mt_rand(): max(2) is smaller than min(5) in /mnt/hgfs/data/ciao-php/ciao/test.php on line 6

I haven't noticed this incompatibility in the backward incompatible changes list.

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

for ($i = 0; $i < 5; $i++) {
    var_dump(mt_rand(160, 0)); 
    echo "\n";
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-14 09:36 UTC] nicolas dot giraud at actiane dot com
It seems this Warning does not appear in PHP 5.3.3:

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

echo PHP_VERSION . "\n";
error_reporting(-1);
ini_set('display_errors', 1);
for ($i = 0; $i < 5; $i++) {
    var_dump(mt_rand(160, 0)); 
    echo "\n";
}

?>

Result:
---------------
5.3.3-7+squeeze8
int(89)

int(95)

int(134)

int(154)

int(157)


Maybe the warning was added in PHP 5.3.8 and further...
 [2012-11-15 17:48 UTC] googleguy@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-11-15 17:48 UTC] googleguy@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: googleguy
 [2012-11-15 17:49 UTC] googleguy@php.net
-Package: *Math Functions +Package: Documentation problem
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 03:00:02 2026 UTC