php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35662 The 'rand()' function returns only few different values
Submitted: 2005-12-13 20:53 UTC Modified: 2005-12-13 22:34 UTC
From: jullrich at sans dot org Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 4.4.1 OS: Linux 2.6 (RedHat)
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: jullrich at sans dot org
New email:
PHP Version: OS:

 

 [2005-12-13 20:53 UTC] jullrich at sans dot org
Description:
------------
The 'rand' function is far from random. For example, the
following code:

<?= rand(10000000000,99999999999); ?>

only generates values that start with '12','13' or '14'.
Out of 1500 times the function is called, only approx. 1000 different values are generated.






Reproduce code:
---------------
<?= rand(10000000000,99999999999); ?>

Expected result:
----------------
somewhat random numbers between 10000000000 and 99999999999.


Actual result:
--------------
all numbers are in the range from 120000000000 to 150000000000 and frequently repeat (about 25% of the numbers
are repeats).

Sample:

1379409636
1275750426
1248288483
1398136354
1404047798
1292410973
1405794269
1278267510
1225653649
 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-13 20:58 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip


 [2005-12-13 22:20 UTC] jullrich at sans dot org
Getting the same problem with the php 5.1-latest.

Code used to test:
<?=

#    1234567890 1234567890
rand(1000000000,9999999999);

?>

Wrapper to generate 100 numbers:

for b in `seq 1 100`; do php -e x.php >> numbers; done

Quick summary of first two digits:
10... 21 times
11... 21 times
12... 13 times
13... 24 times
14...  3 times

so still the same problem. Only numbers from 10... to 14... are generated.
 [2005-12-13 22:29 UTC] jullrich at sans dot org
'rand' works fine up to 9 digits. I just did more tests and the bug only shows up for >= 10 digits.
 [2005-12-13 22:34 UTC] fmk@php.net
The rand functions takes to integers as arguments
when the second argument 9999999999 is cast to an integer it results in 1410065407. This is why you only get values starting with 10, 11, 12, 13 and 14
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 04:01:23 2020 UTC