|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 06:00:01 2025 UTC |
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