{"record":{"id":"14d586dc1cf81a4e","repo":"gchq/CyberChef","slug":"min-cannot-be-larger-than-max","errorCode":null,"errorMessage":"Min cannot be larger than Max.","messagePattern":"Min cannot be larger than Max\\.","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/PseudoRandomIntegerGenerator.mjs","lineNumber":91,"sourceCode":"    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const [numInts, minInt, maxInt, delimiter, outputType] = args;\n\n        if (minInt === null || maxInt === null) return \"\";\n\n        const min = Math.ceil(minInt);\n        const max = Math.floor(maxInt);\n        const delim = Utils.charRep(delimiter || \"Space\");\n\n        if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) {\n            throw new OperationError(\"Min and Max must be between `-(2^53 - 1)` and `2^53 - 1`.\");\n        }\n        if (min > max) {\n            throw new OperationError(\"Min cannot be larger than Max.\");\n        }\n        const range = max - min + 1; // inclusive range\n        if (range > PseudoRandomIntegerGenerator.MAX_RANGE) {\n            throw new OperationError(\"Range between Min and Max cannot be larger than `2^53`\");\n        }\n\n        // as large as possible while divisible by range\n        const rejectionThreshold = PseudoRandomIntegerGenerator.MAX_RANGE - (PseudoRandomIntegerGenerator.MAX_RANGE % range);\n        const output = [];\n        for (let i = 0; i < numInts; i++) {\n            const result = this._generateRandomValue(rejectionThreshold);\n            const intValue = min + (result % range);\n\n            switch (outputType) {\n                case \"Hex\":\n                    output.push(intValue.toString(16));\n                    break;\n                case \"Decimal\":","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PseudoRandomIntegerGenerator.mjs#L73-L109","documentation":"After confirming both bounds are safe integers, the generator checks that Min is not greater than Max. A reversed range is meaningless for the inclusive sampling loop, so it throws rather than silently producing nothing. Note the bounds are first ceil'd/floor'd, so a fractional Min/Max pair that crosses after rounding can also trip this.","triggerScenarios":"Setting 'Min Value' higher than 'Max Value' (e.g. min 100, max 10). Also possible with fractional inputs where Math.ceil(min) ends up greater than Math.floor(max) (e.g. min 5.9 -> 6, max 5.1 -> 5).","commonSituations":"Swapping the min/max fields by mistake; fractional bounds that invert after rounding; importing a recipe with transposed args.","solutions":["Set Min to a value <= Max.","If using fractional bounds, account for ceil(Min) and floor(Max) so they do not invert.","Swap the two arg values if they are transposed."],"exampleFix":"// before: min > max\nrun(\"\", [1, 100, 10, \"Space\", \"Decimal\"]);\n\n// after: min <= max\nrun(\"\", [1, 10, 100, \"Space\", \"Decimal\"]);","handlingStrategy":"validation","validationCode":"const min = Math.ceil(minInt), max = Math.floor(maxInt);\nif (min > max) throw new Error(`Min (${min}) must be <= Max (${max})`);","typeGuard":"function isOrderedRange(minInt, maxInt) {\n  return Math.ceil(minInt) <= Math.floor(maxInt);\n}","tryCatchPattern":"try {\n  return prng.run(input, [n, minInt, maxInt, delim, out]);\n} catch (e) {\n  if (e.message === \"Min cannot be larger than Max.\") {\n    // swap bounds if transposed, then retry\n  }\n  throw e;\n}","preventionTips":["Set Min <= Max.","Watch fractional bounds that invert after ceil/floor.","Double-check transposed arg order."],"tags":["crypto","random","numeric","input-validation","arguments"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}