W3cubDocs

/PHP

mt_getrandmax

(PHP 4, PHP 5, PHP 7)

mt_getrandmaxShow largest possible random value

Description

mt_getrandmax ( ) : int

Returns the maximum value that can be returned by a call to mt_rand().

Return Values

Returns the maximum random value returned by a call to mt_rand() without arguments, which is the maximum value that can be used for its max parameter without the result being scaled up (and therefore less random).

Examples

Example #1 Calculate a random floating-point number

<?php
function randomFloat($min = 0, $max = 1) {
    return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}

var_dump(randomFloat());
var_dump(randomFloat(2, 20));
?>

The above example will output something similar to:

float(0.91601131712832)
float(16.511210331931)

See Also

  • mt_rand() - Generate a random value via the Mersenne Twister Random Number Generator
  • mt_srand() - Seeds the Mersenne Twister Random Number Generator
  • getrandmax() - Show largest possible random value

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/function.mt-getrandmax.php