#49701 [Opn]: rand always returns min value

From: Date: Mon, 28 Sep 2009 19:21:23 +0000
Subject: #49701 [Opn]: rand always returns min value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143011@lists.php.net to get a copy of this message
 ID:               49701
 Updated by:       sjoerd@php.net
 Reported By:      applezinc at gmail dot com
 Status:           Open
 Bug Type:         Math related
 Operating System: slackware linux 12.2
 PHP Version:      5.2.11
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[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



Thread (4 messages)

« previous php.bugs (#143011) next »