W3docs

mt_getrandmax()

Today, we will discuss the mt_getrandmax() function in PHP. This function is used to get the maximum value that can be returned by the mt_rand() function.

The mt_getrandmax() function in PHP returns the largest possible integer that mt_rand() can generate. It takes no arguments and returns a constant value for the lifetime of the script, so it's mainly used as a known upper bound when you scale or normalize random numbers.

This page covers what the function returns, the syntax, why the value matters, how to turn it into a random float, common pitfalls, and how it differs from the older getrandmax().

What is the mt_getrandmax() Function?

mt_getrandmax() is a built-in PHP function that returns an int — the largest value mt_rand() can produce. Because mt_rand() draws from the Mersenne Twister pseudo-random number generator, this function exposes the upper bound of that generator's output range.

The value is platform-dependent but, on every modern PHP build, it is 2147483647 (2^31 - 1). It does not grow on 64-bit systems — mt_rand() always works within the 32-bit range, so mt_getrandmax() returns the same number everywhere. Treat it as a constant for the duration of your script.

Syntax

mt_getrandmax(): int
  • Parameters: none.
  • Return value: the maximum integer mt_rand() can return.

How to Use the mt_getrandmax() Function

Using mt_getrandmax() is straightforward — call it with no arguments. Here is a basic example:

Basic Usage

php— editable, runs on the server

The code above retrieves the upper limit and stores it in a variable for later use.

Defining an explicit range You can combine mt_getrandmax() with mt_rand() to make the full output range explicit. The two calls below are equivalent — calling mt_rand() with no arguments already returns a value between 0 and mt_getrandmax():

<?php
$max = mt_getrandmax();
$randomNumber = mt_rand(0, $max);
echo $randomNumber, "\n";   // some value in 0..2147483647

echo mt_rand(), "\n";       // identical range, no arguments needed
?>

Generating a Random Float Between 0 and 1

The most common real use of mt_getrandmax() is normalizing a random integer into a float in the range [0, 1). Divide the random integer by the maximum:

<?php
$ratio = mt_rand() / (mt_getrandmax() + 1);
echo $ratio; // e.g. 0.4173...
?>

Adding 1 to the divisor keeps the result strictly below 1. To scale into any range [$min, $max], multiply the ratio by the span:

<?php
$min = 5;
$max = 25;
$value = $min + $ratio * ($max - $min);

Common Pitfalls

  • It is not a setter. mt_getrandmax() only reads the upper bound — there is no way to change it. To constrain output, pass min and max to mt_rand() instead.
  • It takes no arguments. Passing any will raise an ArgumentCountError in PHP 8+.
  • Not cryptographically secure. Neither mt_rand() nor its range is safe for passwords, tokens, or keys. For security-sensitive randomness use random_int() or random_bytes().
  • Avoid the modulo trick for ranges. mt_rand() % $n introduces bias toward smaller values. Prefer mt_rand(0, $n - 1).

mt_getrandmax() vs getrandmax()

PHP ships a parallel pair of generators. getrandmax() reports the limit for the older rand() function, while mt_getrandmax() reports the limit for mt_rand(). Since PHP 7.1, rand() and mt_rand() use the same Mersenne Twister engine internally, so both functions return 2147483647 — but you should still pair each *getrandmax() with its matching generator for clarity.

Conclusion

mt_getrandmax() returns the upper bound of PHP's Mersenne Twister generator (2147483647 on modern builds). Its main value is as a divisor for producing normalized random floats and scaled ranges. For secure randomness reach for random_int() instead, and remember the function is read-only — use mt_rand($min, $max) to actually constrain results. See also mt_srand() for seeding the generator.

Practice

Practice
What does the PHP mt_getrandmax() function do?
What does the PHP mt_getrandmax() function do?
Was this page helpful?