From: Wil Mahan Date: Mon, 11 Oct 2004 17:34:39 +0000 (+0000) Subject: Bug 589: make random selection slightly more random. PHP's X-Git-Tag: 1.5.0alpha1~1591 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=2a03980093b5168a834bbf65e820c5400e29b21b;p=lhc%2Fweb%2Fwiklou.git Bug 589: make random selection slightly more random. PHP's mt_rand() function has a maximum value of 2^31-1, which is small enough that duplicate values can occur due to the Birthday paradox, e.g. on the English Wikipedia. To fix this, add a wfRandom() function that calls mt_rand() twice to get the desired amount of randomness. --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 971e671db9..276a863134 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -124,6 +124,22 @@ function wfSeedRandom() { } } +/** + * Get a random decimal value between 0 and 1, in a way + * not likely to give duplicate values for any realistic + * number of articles. + * + * @return string + */ +function wfRandom() { + # The maximum random value is "only" 2^31-1, so get two random + # values to reduce the chance of dupes + $max = mt_getrandmax(); + $rand = number_format( mt_rand() * mt_rand() + / $max / $max, 12, '.', '' ); + return $rand; +} + /** * We want / and : to be included as literal characters in our title URLs. * %2F in the page titles seems to fatally break for some reason. diff --git a/includes/SpecialRandompage.php b/includes/SpecialRandompage.php index a8c5ed3729..4066c6539c 100644 --- a/includes/SpecialRandompage.php +++ b/includes/SpecialRandompage.php @@ -20,10 +20,9 @@ function wfSpecialRandompage() { # # Using a literal constant means the whole thing gets optimized on # the index, and the comparison is both fast and fair. - $rand = mt_rand() / mt_getrandmax(); # interpolation and sprintf() can muck up with locale-specific decimal separator - $randstr = number_format( $rand, 12, ".", "" ); + $randstr = wfRandom(); $db =& wfGetDB( DB_SLAVE ); $use_index = $db->useIndexClause( 'cur_random' ); diff --git a/includes/SpecialUndelete.php b/includes/SpecialUndelete.php index 3e4e2d92af..02527604d8 100644 --- a/includes/SpecialUndelete.php +++ b/includes/SpecialUndelete.php @@ -167,7 +167,7 @@ class PageArchive { $redirect = MagicWord::get( MAG_REDIRECT ); $redir = $redirect->matchStart( $text ) ? 1 : 0; - $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' ); + $rand = wfRandom(); $dbw->insertArray( 'cur', array( 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ), 'cur_namespace' => $namespace, diff --git a/includes/Title.php b/includes/Title.php index 9eb2552a6a..ef1155375d 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1441,7 +1441,7 @@ class Title { $now = $dbw->timestamp(); $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) ); wfSeedRandom(); - $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' ); + $rand = wfRandom(); # Rename cur entry $dbw->updateArray( 'cur',