php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49701 rand always returns min value
Submitted: 2009-09-28 17:31 UTC Modified: 2009-09-29 15:23 UTC
From: applezinc at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.11 OS: slackware linux 12.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: applezinc at gmail dot com
New email:
PHP Version: OS:

 

 [2009-09-28 17:31 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-28 19:21 UTC] 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-29 12:41 UTC] 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-29 15:23 UTC] applezinc at gmail dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 14:01:33 2024 UTC