bchavez/Bogus · error · ArgumentException
The specified range does not contain any even numbers.
Error message
The specified range does not contain any even numbers.
What it means
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.
Source
Thrown at Source/Bogus/Randomizer.cs:124
int bottomHalf = (sample2 >> 8) & 0xFFFF;
return unchecked((topHalf << 16) | bottomHalf);
}
}
/// <summary>
/// Returns a random even number. If the range does not contain any even numbers, an <see cref="ArgumentException" /> is thrown.
/// </summary>
/// <param name="min">Lower bound, inclusive</param>
/// <param name="max">Upper bound, inclusive</param>
/// <exception cref="ArgumentException">Thrown if it is impossible to select an odd number satisfying the specified range.</exception>
public int Even(int min = 0, int max = 1)
{
// Ensure that we have a valid range.
if( min > max )
throw new ArgumentException($"The min/max range is invalid. The minimum value '{min}' is greater than the maximum value '{max}'.", nameof(max));
if( ((min & 1) == 1) && (max - 1 < min) )
throw new ArgumentException("The specified range does not contain any even numbers.", nameof(max));
// Adjust the range to ensure that we always get the same number of even values as odd values.
// For example,
// if the input is min = 1, max = 3, the new range should be min = 2, max = 3.
// if the input is min = 2, max = 3, the range should remain min = 2, max = 3.
min = (min + 1) & ~1;
max = max | 1;
if( min > max )
return min;
// Strip off the last bit of a random number to make the number even.
return Number(min, max) & ~1;
}
/// <summary>
/// Returns a random odd number. If the range does not contain any odd numbers, an <see cref="ArgumentException" /> is thrown.
/// </summary>View on GitHub (pinned to 6ece18c5c2)
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.
Example fix
// before var n = f.Random.Even(3, 3); // after var n = f.Random.Even(2, 4);
Defensive patterns
Strategy: validation
Validate before calling
static int SafeEven(Bogus.Randomizer r, int min, int max) {
bool hasEven = ((min & 1) == 0) || (max - 1 >= min);
if (!hasEven) throw new InvalidOperationException($"range {min}..{max} has no even number");
return r.Even(min, max);
} Type guard
bool RangeHasEven(int min, int max) => ((min & 1) == 0) || (max >= min + 1);
Try / catch
try { return r.Even(min, max); }
catch (ArgumentException ex) when (ex.Message.Contains("does not contain any even")) {
// widen range or fall back
return r.Even(min, max + 1);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Passing a degenerate single-value range that happens to be odd; dynamically computed narrow ranges.
Related errors
- The min/max range is invalid. The minimum value '{min}' is g
- The specified range does not contain any odd numbers.
- max digit can't be lager than 9 or smaller than 0
- The array is empty. There are no items to select.
- The list is empty. There are no items to select.
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/d45052139211a81c.
Report an issue: GitHub.