php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75170 mt_rand() bias on 64-bit machines
Submitted: 2017-09-07 17:55 UTC Modified: -
From: nikic@php.net Assigned:
Status: Closed Package: Math related
PHP Version: 7.1.9 OS:
Private report: No CVE-ID: None
 [2017-09-07 17:55 UTC] 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%

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-07 18:06 UTC] nikic@php.net
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
 [2017-09-07 18:06 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC