paragonie/random_compat · error · Error

Minimum value must be less than or equal to the maximum…

Error message

Minimum value must be less than or equal to the maximum value

What it means

random_int() validates that the range is non-degenerate: if $min is greater than $max it throws this Error. An empty or inverted range has no valid integer to return, and random() semantics require min <= max (equal values simply return $min).

Solutions

  1. Normalize the range before calling: if ($min > $max) [$min, $max] = [$max, $min];
  2. Validate user-supplied ranges and reject inverted ones with a domain error.
  3. Fix call sites that pass arguments in the wrong order.
  4. Add an assertion or unit test around range-producing logic.

Example fix

// before
$pick = random_int($max, $min); // arguments swapped
// after
if ($min > $max) {
    [$min, $max] = [$max, $min];
}
$pick = random_int($min, $max);
Defensive patterns

Strategy: validation

Validate before calling

if ($min > $max) {
    [$min, $max] = [$max, $min]; // or throw InvalidArgumentException
}

Type guard

null

Try / catch

try {
    $n = random_int($min, $max);
} catch (Error $e) {
    if (strpos($e->getMessage(), 'less than or equal to') !== false) {
        // inverted range: normalize and retry once
        [$min, $max] = [$max, $min];
        $n = random_int($min, $max);
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling random_int(10, 1) or random_int($b, $a) where variables are swapped or computed in the wrong order — the '$min > $max' check at lib/random_int.php:77 fires. Exercise by tests testBirthday, testDistribution, testCoverage, testOutput, testRandomRange.

Common situations: Swapped arguments when the caller confused parameter order, ranges computed from dynamic data where min/max were assigned in the wrong branch, or user-supplied ranges without normalization.

Related errors


AI-assisted analysis of paragonie/random_compat@b5d188cc9d (2026-09-13). Data as JSON: /api/errors/6f4617372bd9c6e0. Report an issue: GitHub.

Appendix: source

Thrown at lib/random_int.php:77

            );
        }

        try {
            /** @var int $max */
            $max = RandomCompat_intval($max);
        } catch (TypeError $ex) {
            throw new TypeError(
                'random_int(): $max must be an integer'
            );
        }

        /**
         * Now that we've verified our weak typing system has given us an integer,
         * let's validate the logic then we can move forward with generating random
         * integers along a given range.
         */
        if ($min > $max) {
            throw new Error(
                'Minimum value must be less than or equal to the maximum value'
            );
        }

        if ($max === $min) {
            return (int) $min;
        }

        /**
         * Initialize variables to 0
         *
         * We want to store:
         * $bytes => the number of random bytes we need
         * $mask => an integer bitmask (for use with the &) operator
         *          so we can minimize the number of discards
         */
        $attempts = $bits = $bytes = $mask = $valueShift = 0;
        /** @var int $attempts */

View on GitHub (pinned to b5d188cc9d)