{"record":{"id":"c29cafc9240fffd3","repo":"gchq/CyberChef","slug":"range-between-min-and-max-cannot-be-larger-than-2","errorCode":null,"errorMessage":"Range between Min and Max cannot be larger than `2^53`","messagePattern":"Range between Min and Max cannot be larger than `2\\^53`","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/PseudoRandomIntegerGenerator.mjs","lineNumber":95,"sourceCode":"     */\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\":\n                    output.push(intValue.toString(10));\n                    break;\n                case \"Raw\":\n                default:","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PseudoRandomIntegerGenerator.mjs#L77-L113","documentation":"Even when both bounds are safe integers and Min <= Max, the inclusive range (Max - Min + 1) must not exceed Number.MAX_SAFE_INTEGER (2^53 - 1). The rejection-sampling logic and 53-bit random stitching cannot represent a larger range, so the op refuses. This trips when the two safe-integer bounds sit near opposite ends of the safe-integer spectrum.","triggerScenarios":"Min near -(2^53 - 1) and Max near 2^53 - 1, so Max - Min + 1 exceeds 2^53 - 1. For example min = -Number.MAX_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER.","commonSituations":"Trying to span the entire safe-integer spectrum; generating IDs over a very wide range without considering the implementation limit.","solutions":["Narrow the range so (Max - Min + 1) <= 2^53 - 1.","If you need the full spectrum, split into multiple smaller-range calls and combine.","Use a dedicated BigInt-capable generator for ranges beyond MAX_SAFE_INTEGER."],"exampleFix":"// before: full safe-integer span\nrun(\"\", [1, -Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, \"Space\", \"Decimal\"]);\n\n// after: bounded range within limit\nrun(\"\", [1, 0, 99, \"Space\", \"Decimal\"]);","handlingStrategy":"validation","validationCode":"const min = Math.ceil(minInt), max = Math.floor(maxInt);\nconst range = max - min + 1;\nif (range > Number.MAX_SAFE_INTEGER) {\n  throw new Error(`Range ${range} exceeds 2^53 - 1; narrow the bounds`);\n}","typeGuard":"function rangeWithinSafeIntegerLimit(minInt, maxInt) {\n  return Math.floor(maxInt) - Math.ceil(minInt) + 1 <= Number.MAX_SAFE_INTEGER;\n}","tryCatchPattern":"try {\n  return prng.run(input, [n, minInt, maxInt, delim, out]);\n} catch (e) {\n  if (e.message.includes(\"cannot be larger than `2^53`\")) {\n    // narrow the range or split into multiple calls\n  }\n  throw e;\n}","preventionTips":["Keep (Max - Min + 1) within 2^53 - 1.","Split very wide ranges into multiple smaller calls.","Use a BigInt-capable generator for full int64 ranges."],"tags":["crypto","random","numeric","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}