PHP getrandmax() Function
The PHP getrandmax() function returns the maximum value that can be returned by a call to rand() function.
Syntax
getrandmax()
Parameters
No parameter is required.
Return Value
Returns the largest possible random value returned by rand() function.
Example:
In the example below, getrandmax() function is used to show the maximum value that can be returned by rand().
<?php echo "getrandmax() = ".getrandmax()."\n"; ?>
The output of the above code will be:
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 rand()/getrandmax()."\n"; } ?>
The output of the above code will be:
0.094408831137423 0.7835767240187 0.86054130218017 0.52452071501153 0.0097432336815369 0.4829214077829 0.22035302883962 0.3247421757899 0.8502653929639 0.79505650689595
❮ PHP Math Reference