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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
43 + 8 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 05:01:28 2024 UTC