bug75170.phpt 842 B

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