{"record":{"id":"5e3ca0730ed78f9a","repo":"bchavez/Bogus","slug":"the-min-max-range-is-invalid-the-minimum-value","errorCode":null,"errorMessage":"The min/max range is invalid. The minimum value '{min}' is greater than the maximum value '{max}'.","messagePattern":"The min/max range is invalid\\. The minimum value '(.+?)' is greater than the maximum value '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":122,"sourceCode":"\n         int topHalf = (sample1 >> 8) & 0xFFFF;\n         int bottomHalf = (sample2 >> 8) & 0xFFFF;\n\n         return unchecked((topHalf << 16) | bottomHalf);\n      }\n   }\n\n   /// <summary>\n   /// Returns a random even number. If the range does not contain any even numbers, an <see cref=\"ArgumentException\" /> is thrown.\n   /// </summary>\n   /// <param name=\"min\">Lower bound, inclusive</param>\n   /// <param name=\"max\">Upper bound, inclusive</param>\n   /// <exception cref=\"ArgumentException\">Thrown if it is impossible to select an odd number satisfying the specified range.</exception>\n   public int Even(int min = 0, int max = 1)\n   {\n      // Ensure that we have a valid range.\n      if( min > max )\n         throw new ArgumentException($\"The min/max range is invalid. The minimum value '{min}' is greater than the maximum value '{max}'.\", nameof(max));\n      if( ((min & 1) == 1) && (max - 1 < min) )\n         throw new ArgumentException(\"The specified range does not contain any even numbers.\", nameof(max));\n\n      // Adjust the range to ensure that we always get the same number of even values as odd values.\n      // For example,\n      //   if the input is min = 1, max = 3, the new range should be min = 2, max = 3.\n      //   if the input is min = 2, max = 3, the range should remain min = 2, max = 3.\n      min = (min + 1) & ~1;\n      max = max | 1;\n\n      if( min > max )\n         return min;\n\n      // Strip off the last bit of a random number to make the number even.\n      return Number(min, max) & ~1;\n   }\n\n   /// <summary>","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L104-L140","documentation":"Thrown by Randomizer.Even when min > max. The check is the first validation in Even(); it guards the inclusive range before the parity logic. The exception param is named 'max'.","triggerScenarios":"Calling f.Random.Even(min: 10, max: 2) i.e. swapping bounds or passing a descending range.","commonSituations":"Computing min/max from external inputs where order is not enforced; off-by-one in bound calculation.","solutions":["Ensure min <= max before calling Even.","Normalize bounds: if (min > max) (min, max) = (max, min);","Validate input bounds at the source (config / user input) before reaching the randomizer."],"exampleFix":"// before\nvar n = f.Random.Even(min: hi, max: lo);\n// after\nint lo = Math.Min(hi, lo0), hi = Math.Max(hi, lo0);\nvar n = f.Random.Even(min: lo, max: hi);","handlingStrategy":"validation","validationCode":"static int SafeEven(Bogus.Randomizer r, int min, int max) {\n    if (min > max) (min,max) = (max,min);\n    return r.Even(min, max);\n}","typeGuard":null,"tryCatchPattern":"try { return r.Even(min, max); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"minimum value\") && ex.Message.Contains(\"greater\")) {\n    return r.Even(Math.Min(min,max), Math.Max(min,max));\n}","preventionTips":["Normalize min/max ordering at the call site.","Validate bound order from external inputs.","Wrap Even in a helper that sorts bounds."],"tags":["bogus","randomizer","even","range-validation"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}