{"record":{"id":"d45052139211a81c","repo":"bchavez/Bogus","slug":"the-specified-range-does-not-contain-any-even-numb","errorCode":null,"errorMessage":"The specified range does not contain any even numbers.","messagePattern":"The specified range does not contain any even numbers\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":124,"sourceCode":"         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>\n   /// Returns a random odd number. If the range does not contain any odd numbers, an <see cref=\"ArgumentException\" /> is thrown.\n   /// </summary>","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L106-L142","documentation":"Thrown by Randomizer.Even when the range contains no even numbers. The condition ((min & 1) == 1) && (max - 1 < min) means min is odd and max is less than min+1, i.e. the range is a single odd number or otherwise has no even value.","triggerScenarios":"Calling f.Random.Even(min: 3, max: 3) (single odd value), or any range like Even(3,3) where min is odd and max-1 < min.","commonSituations":"Passing a degenerate single-value range that happens to be odd; dynamically computed narrow ranges.","solutions":["Widen the range so at least one even number is included (e.g. ensure max >= min+1 when min is odd).","Validate the range before calling: require that some even exists (max > min || (min & 1) == 0).","Fall back to Odd() or Number() if even numbers are not strictly required."],"exampleFix":"// before\nvar n = f.Random.Even(3, 3);\n// after\nvar n = f.Random.Even(2, 4);","handlingStrategy":"validation","validationCode":"static int SafeEven(Bogus.Randomizer r, int min, int max) {\n    bool hasEven = ((min & 1) == 0) || (max - 1 >= min);\n    if (!hasEven) throw new InvalidOperationException($\"range {min}..{max} has no even number\");\n    return r.Even(min, max);\n}","typeGuard":"bool RangeHasEven(int min, int max) => ((min & 1) == 0) || (max >= min + 1);","tryCatchPattern":"try { return r.Even(min, max); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"does not contain any even\")) {\n    // widen range or fall back\n    return r.Even(min, max + 1);\n}","preventionTips":["Widen ranges to include at least one even number.","Validate degenerate single-value ranges before sampling.","Fall back to Number() when parity is not essential."],"tags":["bogus","randomizer","even","empty-range"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}