Bug #75170 [Opn->Csd]: mt_rand() bias on 64-bit machines
Edit report at https://bugs.php.net/bug.php?id=75170&edit=1
ID: 75170
Updated by: nikic@php.net
Reported by: nikic@php.net
Summary: mt_rand() bias on 64-bit machines
-Status: Open
+Status: Closed
Type: Bug
Package: Math related
PHP Version: 7.1.9
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fd07302024bc47082b13b32217147fd39d1e9e61
Log: Fixed bug #75170
Previous Comments:
------------------------------------------------------------------------
[2017-09-07 17:55:04] nikic@php.net
Description:
------------
A bias in the PHP 7.1 implementation of mt_rand() on 64-bit systems has been reported by Solar
Designer on the mailing list. See https://externals.io/message/100229 for more
information.
Test script:
---------------
<?php
mt_srand(1234567890);
$total = 100000;
$max = 0x66666666;
$halves[0] = $halves[1] = 0;
for ($i = 0; $i < $total; $i++) {
$halves[mt_rand(0, $max - 1) / ($max / 2)]++;
}
printf("%.1f%% vs. %.1f%%\n", 100. * $halves[0] / $total, 100. * $halves[1] / $total);
?>
Expected result:
----------------
50.0% vs. 50.0%
Actual result:
--------------
60.0% vs. 40.0%
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75170&edit=1
Thread (2 messages)