{"record":{"id":"6f4617372bd9c6e0","repo":"paragonie/random_compat","slug":"minimum-value-must-be-less-than-or-equal-to-the-maximum","errorCode":null,"errorMessage":"Minimum value must be less than or equal to the maximum value","messagePattern":"Minimum value must be less than or equal to the maximum value","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/random_int.php","lineNumber":77,"sourceCode":"            );\n        }\n\n        try {\n            /** @var int $max */\n            $max = RandomCompat_intval($max);\n        } catch (TypeError $ex) {\n            throw new TypeError(\n                'random_int(): $max must be an integer'\n            );\n        }\n\n        /**\n         * Now that we've verified our weak typing system has given us an integer,\n         * let's validate the logic then we can move forward with generating random\n         * integers along a given range.\n         */\n        if ($min > $max) {\n            throw new Error(\n                'Minimum value must be less than or equal to the maximum value'\n            );\n        }\n\n        if ($max === $min) {\n            return (int) $min;\n        }\n\n        /**\n         * Initialize variables to 0\n         *\n         * We want to store:\n         * $bytes => the number of random bytes we need\n         * $mask => an integer bitmask (for use with the &) operator\n         *          so we can minimize the number of discards\n         */\n        $attempts = $bits = $bytes = $mask = $valueShift = 0;\n        /** @var int $attempts */","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/paragonie/random_compat/blob/b5d188cc9d5e02f94d2c41da23093f1ef557c5b1/lib/random_int.php#L59-L95","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Normalize the range before calling: if ($min > $max) [$min, $max] = [$max, $min];","Validate user-supplied ranges and reject inverted ones with a domain error.","Fix call sites that pass arguments in the wrong order.","Add an assertion or unit test around range-producing logic."],"exampleFix":"// before\n$pick = random_int($max, $min); // arguments swapped\n// after\nif ($min > $max) {\n    [$min, $max] = [$max, $min];\n}\n$pick = random_int($min, $max);","handlingStrategy":"validation","validationCode":"if ($min > $max) {\n    [$min, $max] = [$max, $min]; // or throw InvalidArgumentException\n}","typeGuard":"null","tryCatchPattern":"try {\n    $n = random_int($min, $max);\n} catch (Error $e) {\n    if (strpos($e->getMessage(), 'less than or equal to') !== false) {\n        // inverted range: normalize and retry once\n        [$min, $max] = [$max, $min];\n        $n = random_int($min, $max);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Normalize min/max ordering in a shared helper before calling random_int().","Double-check argument order at call sites (min first, max second).","Validate user-supplied ranges instead of silently swapping when order matters.","Add tests covering inverted-range inputs."],"tags":["php","argument-out-of-range","randomness","validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"b5d188cc9d5e02f94d2c41da23093f1ef557c5b1","analyzedAt":"2026-09-13T16:12:09.755Z","contentChangedAt":"2026-09-13T16:12:09.755Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}