php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43108 rand generates predictable results
Submitted: 2007-10-26 09:44 UTC Modified: 2007-11-07 18:59 UTC
From: cetra3 at hotmaill dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.4 OS: Windows XP
Private report: No CVE-ID: None
 [2007-10-26 09:44 UTC] cetra3 at hotmaill dot com
Description:
------------
The function Rand will produce predictable results.

Reproduce code:
---------------
<?php

$array_random = array_fill(1,10,1);

for($i=0;$i<100000000;$i++) {

$array_random[rand(1,10)]++;

}

print_r($array_random);

?>

Expected result:
----------------
This should produce an array which counts how many times each number is picked at random.  It should return a different result each time.

Actual result:
--------------
Instead of getting purely random responses, we get predictable ones, in this example there will be tainted results, with numbers 5 and 10 producing lower counts:

Array
(
    [1] => 10000594
    [2] => 10000610
    [3] => 10000633
    [4] => 10000589
    [5] => 9997562
    [6] => 10000606
    [7] => 10000568
    [8] => 10000620
    [9] => 10000645
    [10] => 9997583
)

You will always see these types of return values.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-31 17:01 UTC] crescentfreshpot at yahoo dot com
http://en.wikipedia.org/wiki/Principle_of_indifference

A hundred million times is a shitload dude.
 [2007-10-31 17:14 UTC] crescentfreshpot at yahoo dot com
> with numbers 5 and 10 producing lower counts

Oh I see now.
 [2007-11-07 18:59 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

There is nothing PHP-specific about rand() as it relies on libc's rand.

Remember that those functions can only produce pseudo-random numbers. Patterns can emerge because of that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 10:01:30 2024 UTC