php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70003 mt_rand producing non random values
Submitted: 2015-07-06 18:47 UTC Modified: 2015-07-09 16:00 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: cjds at live dot com Assigned: bishop (profile)
Status: Duplicate Package: Math related
PHP Version: 5.6Git-2015-07-06 (Git) OS: Windows/ Linux 32/64 bits
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cjds at live dot com
New email:
PHP Version: OS:

 

 [2015-07-06 18:47 UTC] cjds at live dot com
Description:
------------
---
From manual page: http://www.php.net/function.mt-rand
---

PHPs mt_rand function fails on 32 bit machines. It produces a certain value more than half the time. When the difference between the values are higher than the max_rand value then the system will return a certain value more than half the time. 

For example in the code given the system returns `1` more than 5000 times while generating 10000 random numbers

Test script:
---------------
$count=0;
for ($i = 0; $i <= 10000; $i++) {
    $rand=mt_rand(-2147483608,2147483647); 
    if($rand==1){$count++;}

}
echo('mt_getrandmax()='.mt_getrandmax().' count='.$count);

Expected result:
----------------
mt_getrandmax()=2147483647  count=0

Actual result:
--------------
mt_getrandmax()=2147483647  count=5034

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-07 10:55 UTC] cmb@php.net
Seems to have the same cause as bug #63174.
 [2015-07-08 21:36 UTC] yohgaki@php.net
-Operating System: Windows/ Linux 32 bits +Operating System: Windows/ Linux 32/64 bits
 [2015-07-08 21:36 UTC] yohgaki@php.net
Simple experiment shows it happens on 64 bit linux also

[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-3664030011737767936[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-676576817930305536[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-7677239052164136960[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-6266440968006795264[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-5353380012353388544[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
0[yohgaki@dev github-php-src]$ php -r 'echo mt_rand(PHP_INT_MIN, PHP_INT_MAX);'
-6517772635873476608[yohgaki@dev github-php-src]$ php -v
PHP 7.0.0-dev (cli) (built: Jul  9 2015 06:32:00) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
 [2015-07-09 15:59 UTC] bishop@php.net
-Assigned To: +Assigned To: bishop
 [2015-07-09 16:00 UTC] bishop@php.net
-Status: Assigned +Status: Duplicate
 [2015-07-09 16:00 UTC] bishop@php.net
Duplicated by #63174.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 08:01:29 2024 UTC