mt_getrandmax

Returns the largest integer mt_rand() can produce when called with no arguments.

Signature
mt_getrandmax(): int
  • Added in PHP 4.0

Return value

intThe maximum value returned by mt_rand() when called without arguments: 2147483647 on every platform.

mt_getrandmax() takes no arguments and returns 2147483647 — that is 2 ** 31 - 1. PHP implements the 32-bit Mersenne Twister, so this value is the same on every platform and every build, 32-bit or 64-bit. Since PHP 7.1, getrandmax() returns the identical number because rand() is an alias of mt_rand().

What it is actually for

Two things. First, generating a random float: mt_rand() / mt_getrandmax() gives a value between 0.0 and 1.0 inclusive, which is the classic way to make a random probability before Random\Randomizer::getFloat() existed.

Second, it is the largest $max you can request from mt_rand() without the result being scaled. Ask for a wider range and the generator stretches its 32-bit output to fit, which leaves gaps — not every integer in the range becomes reachable, and the distribution is no longer uniform in a useful sense. If you need a random value across the full 64-bit integer range, use random_int() or Random\Randomizer.

Not a security boundary

The ceiling says nothing about quality. Mersenne Twister output is predictable at any range width.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Produce a random float between 0 and 1 for a probability check
  • Verify that a requested random range is not wider than the generator's native output
  • Scale a random draw into an arbitrary floating-point range

Frequently asked questions

What value does mt_getrandmax() return?

Always 2147483647, which is 2 ** 31 - 1. PHP uses the 32-bit Mersenne Twister, so the value is identical on 32-bit and 64-bit platforms.

What is the difference between mt_getrandmax() and getrandmax()?

Nothing since PHP 7.1, when rand() became an alias of mt_rand(); both functions return 2147483647. mt_getrandmax() is the clearer name to use alongside mt_rand().

How do I get a random float between 0 and 1 in PHP?

Divide mt_rand() by mt_getrandmax(), which yields a value from 0.0 to 1.0 inclusive. On PHP 8.3 and later, Random\Randomizer::getFloat() gives a better-distributed result with explicit interval control.

More examples

Random floats in an arbitrary range

Dividing by mt_getrandmax() normalises the draw to 0-1, and a linear scale maps it onto any range. The last two lines show the same trick used as a straightforward probability gate.

Example · php
<?php
function randFloat(float $min, float $max): float {
    return $min + (mt_rand() / mt_getrandmax()) * ($max - $min);
}

for ($i = 0; $i < 5; $i++) {
    printf("%.4f\n", randFloat(-1.0, 1.0));
}

$p = mt_rand() / mt_getrandmax();
echo $p < 0.3 ? "hit (30% chance)\n" : "miss\n";

Ranges wider than the generator's native output

mt_rand() still answers a 64-bit request, but it scales its 32-bit draw upward, so most integers in that range can never be produced. random_int() generates across the full range directly and is cryptographically secure as well.

Example · php
<?php
echo 'mt_getrandmax: ', mt_getrandmax(), "\n";
echo 'PHP_INT_MAX:   ', PHP_INT_MAX, "\n";

$wide = mt_rand(0, PHP_INT_MAX);
var_dump($wide >= 0 && $wide <= PHP_INT_MAX);  // bool(true)

// random_int() covers the full range without scaling
$secure = random_int(0, PHP_INT_MAX);
var_dump($secure >= 0);  // bool(true)

Checking the distribution of normalised draws

Ten thousand normalised draws split into five buckets land near 2000 each, confirming the division by mt_getrandmax() produces a roughly uniform float. The min(4, ...) guard handles the case where the draw is exactly 1.0.

Example · php
<?php
mt_srand(2024);
$buckets = array_fill(0, 5, 0);
for ($i = 0; $i < 10000; $i++) {
    $p = mt_rand() / mt_getrandmax();
    $buckets[min(4, (int) ($p * 5))]++;
}
print_r($buckets);

Discussion

  • Be the first to comment on this lesson.