PHP Function Reference

PHP mt_getrandmax() Function



The PHP mt_getrandmax() function returns the maximum value that can be returned by a call to mt_rand() function.

Syntax

mt_getrandmax()

Parameters

No parameter is required.

Return Value

Returns the largest possible random value returned by mt_rand() function.

Example:

In the example below, mt_getrandmax() function is used to show the maximum value that can be returned by mt_rand().

<?php
echo "mt_getrandmax() = ".mt_getrandmax()."\n";
?>

The output of the above code will be:

mt_getrandmax() = 2147483647

Example:

Consider one more example where this function is used to generate pseudo-random values between [0, 1].

<?php
//generate 10 pseudo-random value
//between 0 and 1 (inclusive)
for ($i = 1; $i <= 10; $i++) {
  echo mt_rand()/mt_getrandmax()."\n";
}
?>

The output of the above code will be:

0.5429093132461
0.38098700827965
0.61517198831549
0.86471996776048
0.92433259213545
0.17875916891674
0.36788522143284
0.17656602020216
0.067212095049774
0.644872050567

❮ PHP Math Reference