|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-12-16 05:11 UTC] daniel at danielnorton dot com
[2011-12-16 05:38 UTC] daniel at danielnorton dot com
[2011-12-16 06:18 UTC] daniel at danielnorton dot com
[2011-12-16 07:14 UTC] daniel at danielnorton dot com
[2011-12-16 07:14 UTC] daniel at danielnorton dot com
-Summary: mt_rand() poor quality with large magnitude negative
$min
+Summary: mt_rand() only reliable when ($max - $min) <=
mt_getrandmax ()
[2014-07-31 04:01 UTC] datibbaw@php.net
[2015-08-15 10:22 UTC] cmb@php.net
-Status: Open
+Status: Duplicate
-Assigned To:
+Assigned To: cmb
[2015-08-15 10:22 UTC] cmb@php.net
[2015-08-15 10:23 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 16:00:01 2025 UTC |
Description: ------------ Example output from the following script that generates 16 random numbers in the range from -1073741823 to 2147483647. It shows that the number 1073741825 was repeated 4 times, or 25% of the time. This output is typical and the repeated number is always 1073741825. The problem is less the smaller the magnitude of $min. Test script: --------------- <?php printf("PHP_VERSION=%s\n",PHP_VERSION); $min = -(mt_getrandmax()>>1); $max = mt_getrandmax(); $count = isset($argv[1])?(int)$argv[1]:16; printf("min=%d, max=%d, count=%d\n",$min,$max,$count); $repeat_counts = array(); for ($i=0;$i<$count;$i++) { $random = mt_rand($min,$max); if (!isset($repeat_counts[$random])) { $repeat_counts[$random] = 0; } $repeat_counts[$random]++; //printf("%12d%s\n", abs($random),($random<0)?"-":""); } $max_repeat_count = max($repeat_counts); $same_value_max = array(); if ($max_repeat_count > 1) { foreach ($repeat_counts as $value => $repeat_count) { if ($repeat_count >= $max_repeat_count) { $same_value_max[] = $value; } } printf("The following number/s was/were repeated %d times (%s%%): %s\n" ,$max_repeat_count ,number_format(($max_repeat_count/$count)*100.0,1) ,implode(", ",$same_value_max) ); } Expected result: ---------------- PHP_VERSION=5.3.8 min=-1073741823, max=2147483647, count=16 Actual result: -------------- PHP_VERSION=5.3.8 min=-1073741823, max=2147483647, count=16 The following number/s was/were repeated 4 times (25.0%): 1073741825