Bug #70003 [Opn]: mt_rand producing non random values

From: Date: Wed, 08 Jul 2015 21:36:04 +0000
Subject: Bug #70003 [Opn]: mt_rand producing non random values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194232@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70003&edit=1 ID: 70003 Updated by: yohgaki@php.net Reported by: cjds at live dot com Summary: mt_rand producing non random values Status: Open Type: Bug Package: Math related -Operating System: Windows/ Linux 32 bits +Operating System: Windows/ Linux 32/64 bits PHP Version: 5.6Git-2015-07-06 (Git) Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2015-07-07 10:55:14] cmb@php.net Seems to have the same cause as bug #63174. ------------------------------------------------------------------------ [2015-07-06 18:47:43] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70003&edit=1

« previous php.bugs (#194232) next »