©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4, PHP 5)
mt_getrandmax — 显示随机数的最大可能值
返回调用 mt_rand() 所能返回的最大的随机数。
返回调用 mt_rand() 所能返回的最大的随机数。
Example #1 计算一个随机浮点数
<?php
function randomFloat ( $min = 0 , $max = 1 ) {
return $min + mt_rand () / mt_getrandmax () * ( $max - $min );
}
var_dump ( randomFloat ());
var_dump ( randomFloat ( 2 , 20 ));
?>
以上例程的输出类似于:
float(0.91601131712832) float(16.511210331931)
[#1] bishop at php dot net [2015-07-09 16:31:04]
The upper-bound of this value is platform-independent. PHP implements the 32-bit version of the Mersenne Twister ("mt"), so the maximum possible value is 2**31 - 1 (2147483647).
[#2] pablorodriguez85 at gmail dot com [2014-12-12 16:55:30]
In a Mac OS X 10-9-5 the value is also 2147483647
[#3] Anonymous [2013-05-13 12:40:56]
The max on a Windows 7 x64 machine is also 2147483647.
[#4] marcus at synchromedia dot co dot uk [2011-08-10 13:41:57]
On both 32 and 64-bit systems (OS X and Linux), mt_getrandmax() returns 2147483647 for me, i.e. ~2^31.