xref: /PHP-7.4/ext/standard/tests/math/bug75170.phpt (revision fd073020)
1--TEST--
2Bug #75170: mt_rand() bias on 64-bit machines
3--CREDITS--
4Solar Designer in https://externals.io/message/100229
5--FILE--
6<?php
7
8// PHP pre-7.1.0 modulo bias
9mt_srand(1234567890);
10$total = 10000;
11$max = 0x66666666;
12$halves[0] = $halves[1] = 0;
13for ($i = 0; $i < $total; $i++) {
14    $halves[(mt_rand(0, $max - 1) >> 1) & 1]++;
15}
16printf("%.1f%% vs. %.1f%%\n", 100. * $halves[0] / $total, 100. * $halves[1] / $total);
17
18// PHP 7.1.0 to 7.2.0beta2 modulo bias bug found during work
19// on http://www.openwall.com/php_mt_seed/
20mt_srand(1234567890);
21$total = 10000;
22$max = 0x66666666;
23$halves[0] = $halves[1] = 0;
24for ($i = 0; $i < $total; $i++) {
25    $halves[mt_rand(0, $max - 1) / ($max / 2)]++;
26}
27printf("%.1f%% vs. %.1f%%\n", 100. * $halves[0] / $total, 100. * $halves[1] / $total);
28
29?>
30--EXPECT--
3149.5% vs. 50.5%
3250.5% vs. 49.5%
33