{"record":{"id":"b1bd902790873a17","repo":"bchavez/Bogus","slug":"the-specified-range-does-not-contain-any-odd-numbe","errorCode":null,"errorMessage":"The specified range does not contain any odd numbers.","messagePattern":"The specified range does not contain any odd numbers\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":152,"sourceCode":"         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>\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 Odd(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( ((max & 1) == 0) && (min + 1 > max) )\n         throw new ArgumentException(\"The specified range does not contain any odd numbers.\", nameof(max));\n\n      // Special case where the math below breaks.\n      if ( max == int.MinValue )\n         return int.MinValue | 1;\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 = 2, max = 4, 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;\n      max = (max - 1) | 1;\n\n      if( min > max )\n         return min | 1;\n\n      // Ensure that the last bit is set in a random number to make the number odd.\n      return Number(min, max) | 1;\n   }","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L134-L170","documentation":"Thrown by Randomizer.Odd when the range contains no odd numbers. Condition ((max & 1) == 0) && (min + 1 > max) means max is even and the range is too narrow to include an odd (e.g. a single even number).","triggerScenarios":"Calling f.Random.Odd(min: 2, max: 2) - single even value, or Odd(4,4).","commonSituations":"Degenerate single-value ranges where the lone value is even; dynamically narrowed ranges.","solutions":["Widen the range so at least one odd number is included.","Pre-validate: ensure some odd exists (min & 1) == 1 || max > min.","Use Number() or Even() if odd is not strictly required."],"exampleFix":"// before\nvar n = f.Random.Odd(2, 2);\n// after\nvar n = f.Random.Odd(1, 3);","handlingStrategy":"validation","validationCode":"static int SafeOdd(Bogus.Randomizer r, int min, int max) {\n    bool hasOdd = ((max & 1) == 1) || (min + 1 <= max);\n    if (!hasOdd) throw new InvalidOperationException($\"range {min}..{max} has no odd number\");\n    return r.Odd(min, max);\n}","typeGuard":"bool RangeHasOdd(int min, int max) => ((max & 1) == 1) || (max > min);","tryCatchPattern":"try { return r.Odd(min, max); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"does not contain any odd\")) {\n    return r.Odd(min, max + 1);\n}","preventionTips":["Ensure the range includes at least one odd number.","Validate single-value ranges before calling Odd.","Use Number() when parity is not required."],"tags":["bogus","randomizer","odd","empty-range"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}