#49701 [Opn->Bgs]: rand always returns min value

From: Date: Tue, 29 Sep 2009 12:41:53 +0000
Subject: #49701 [Opn->Bgs]: rand always returns min value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143036@lists.php.net to get a copy of this message
ID: 49701 Updated by: jani@php.net Reported By: applezinc at gmail dot com -Status: Open +Status: Bogus Bug Type: Math related Operating System: slackware linux 12.2 PHP Version: 5.2.11 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php . Previous Comments: ------------------------------------------------------------------------ [2009-09-28 19:21:23] sjoerd@php.net Thank you for your bug report. The techical reason this works like this is as follows: rand() takes two integers. When you specify a number bigger than 2^31, it is cast to float. When it is passed to rand(), it is cast to an int. The number 4294967294 then becomes -2. ------------------------------------------------------------------------ [2009-09-28 17:31:31] applezinc at gmail dot com Description: ------------ getrandmax is 2^31. if you put in a number for max greater than that (as the documentation says you can), the function always returns the min. mt_rand is the same. Reproduce code: --------------- echo rand(1,4294967294); echo "\n"; echo getrandmax(); echo "\n"; Expected result: ---------------- random number between 1 and 4294967294. Actual result: -------------- 1 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49701&edit=1

« previous php.bugs (#143036) next »