getrandmax

Returns the largest integer that rand() can possibly return on this system.

Signature
getrandmax(): int
  • Added in PHP 4.0

Return value

intThe largest possible random value returned by rand(). On every mainstream PHP build this is 2147483647.

Changelog

  • PHP 7.1.0rand() became an alias of mt_rand(), so getrandmax() now returns the same value as mt_getrandmax(). On platforms where the old libc RAND_MAX was 32767, the returned value jumped to 2147483647.

getrandmax() reports the ceiling of the unbounded form of rand() — the version you call with no arguments. It takes no parameters and, on current builds, always returns 2147483647, the largest signed 32-bit integer.

When to reach for it

Almost the only honest use left is converting a raw draw into a float: rand() / getrandmax() gives a number between 0 and 1 inclusive, which you can scale into a probability or a percentage. Beyond that, it is a way to document in code that you know where the range stops rather than hard-coding a magic number.

What trips people up

People assume the ceiling tracks the platform's integer size. It does not. Even on a 64-bit build where PHP_INT_MAX is about 9.2 quintillion, rand() stops at just over 2.1 billion. If you need a bigger number, pass explicit bounds to rand() or mt_rand(), or use random_int(), which accepts the full integer range.

The other trap is rand() % $n. Because getrandmax() + 1 is rarely divisible by $n, the low buckets get hit slightly more often. Pass a range to rand($min, $max) instead of taking a modulus.

Example

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

When to use it

  • Convert a raw rand() draw into a 0-to-1 float for a probability check
  • Show a percentage roll in a game or A/B split without hard-coding 2147483647
  • Sanity-check in a diagnostics page which random range the current build offers
  • Explain in code review why rand() % $buckets is biased for a given bucket count

Frequently asked questions

Why does getrandmax() return 2147483647 instead of PHP_INT_MAX?

Because the underlying Mersenne Twister generates 32-bit values, regardless of whether PHP itself is a 64-bit build. To get random integers across the full 64-bit range, use random_int(PHP_INT_MIN, PHP_INT_MAX).

What is the difference between getrandmax and mt_getrandmax?

Nothing on PHP 7.1 and later, since rand() is an alias of mt_rand() and both report the same ceiling. They were different only on older versions where rand() used the platform's libc generator.

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

Use rand() / getrandmax() for a quick non-secure value. For a better-distributed float, use Random\Randomizer::getFloat(0, 1), which avoids the rounding artifacts of dividing two integers.

More examples

Showing the modulo bias

2147483648 is not divisible by 3, so bucket 0 and bucket 1 are reachable from one more starting value than bucket 2. The printed counts are close but not equal, and the gap grows for larger bucket counts — this is why rand(0, 2) is the correct call, not rand() % 3.

Example · php
<?php
$max     = getrandmax();
$buckets = 3;

echo "max: $max\n";
echo 'evenly divisible: ', var_export(($max + 1) % $buckets === 0, true), "\n";

srand(1);
$counts = [0, 0, 0];
for ($i = 0; $i < 30000; $i++) {
    $counts[rand() % $buckets]++;
}
print_r($counts);

Weighted choice from a unit float

Dividing by getrandmax() turns the integer draw into a value between 0 and 1, which lines up directly against cumulative probabilities. The output names the tier the roll landed in; seeding with srand(99) keeps the example reproducible here.

Example · php
<?php
$weights = ['common' => 0.7, 'rare' => 0.25, 'epic' => 0.05];

srand(99);
$roll = rand() / getrandmax();

$cursor = 0.0;
foreach ($weights as $tier => $chance) {
    $cursor += $chance;
    if ($roll <= $cursor) {
        printf("roll %.4f -> %s\n", $roll, $tier);
        break;
    }
}

Discussion

  • Be the first to comment on this lesson.