#49701 [Bgs]: rand always returns min value
| From: | applezinc at gmail dot com | Date: | Tue, 29 Sep 2009 15:23:10 +0000 |
| Subject: | #49701 [Bgs]: rand always returns min value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-143045@lists.php.net to get a copy of this message | ||
ID: 49701
User updated by: applezinc at gmail dot com
Reported By: applezinc at gmail dot com
Status: Bogus
Bug Type: Math related
Operating System: slackware linux 12.2
PHP Version: 5.2.11
New Comment:
No its not bogus. You told me to refer to the documentation, well here
it is:
" If you require a range larger than 32768, specifying min and max
will allow you to create a range larger than this, or consider using
mt_rand() instead."
Your assumption that it is bogus is based upon what? that "int" is 16
bits? int is defined as whatever register size the microprocessor can
handle and it varies.
I understand exactly what the prior poster said. I've been an embedded
software engineer for 15 years. However that's NOT what the
documentation says. So one of them is wrong.
Previous Comments:
------------------------------------------------------------------------
[2009-09-29 12:41:52] jani@php.net
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
.
------------------------------------------------------------------------
[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