bchavez/Bogus · error · ArgumentOutOfRangeException

The {nameof(count)} parameter is {count} and the calculated

Error message

The {nameof(count)} parameter is {count} and the calculated set of enums has a length of {enums.Length}. It is impossible to pick {count} enums from a list of {enums.Length}.

What it means

Thrown by Randomizer.EnumValues<T> (note: ArgumentOutOfRangeException) when count is greater than the available enums after exclusion, or count is negative. The available set is computed from Enum.GetValues(T).Except(exclude), so exclusions shrink the pool.

Source

Thrown at Source/Bogus/Randomizer.cs:706

      T[] enums;
      if( exclude.Length > 0)
      {
         enums = System.Enum.GetValues(typeof(T))
            .OfType<T>()
            .Except(exclude)
            .ToArray();
      }
      else
      {
         enums = System.Enum.GetValues(typeof(T))
            .OfType<T>()
            .Except(exclude)
            .ToArray();
      }

      if( count > enums.Length || count < 0 )
      {
         throw new ArgumentOutOfRangeException(nameof(count), count,
         $"The {nameof(count)} parameter is {count} and the calculated set of enums has a length of {enums.Length}. It is impossible to pick {count} enums from a list of {enums.Length}.");
      }

      return this.ArrayElements(enums, count);
   }

   /// <summary>
   /// Shuffles an IEnumerable source.
   /// </summary>
   public IEnumerable<T> Shuffle<T>(IEnumerable<T> source)
   {
      List<T> buffer = source.ToList();
      for( var i = 0; i < buffer.Count; i++ )
      {
         int j;
         //lock any seed access, for thread safety.
         lock( Locker.Value )
         {

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Compute the available pool first and clamp count: count = Math.Min(count, pool.Length).
  2. Ensure count >= 0 and <= number of non-excluded enum values.
  3. Pass null for count to let Bogus pick a random amount.

Example fix

// before
var vals = f.Random.EnumValues<MyEnum>(count: 10);
// after
int pool = Enum.GetValues<MyEnum>().Length;
var vals = f.Random.EnumValues<MyEnum>(count: Math.Min(10, pool));
Defensive patterns

Strategy: validation

Validate before calling

static T[] SafeEnumValues<T>(Bogus.Randomizer r, int? count, params T[] exclude) where T : struct, Enum {
    int pool = System.Enum.GetValues<T>().Except(exclude).Count();
    int? safe = count is int c ? Math.Clamp(c, 0, pool) : null;
    return r.EnumValues(safe, exclude);
}

Try / catch

try { return r.EnumValues(count, exclude); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("impossible to pick")) {
    return r.EnumValues(null, exclude); // let Bogus pick a random count
}

Prevention

When it happens

Trigger: Calling f.Random.EnumValues(count: 10) on an enum with only 3 members, or EnumValues(-1, ...), or excluding members so count > remaining length.

Common situations: Hardcoding a count larger than the enum size; computing count from unrelated config; excluding members then requesting more than remain.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/e5f637ba3185d113. Report an issue: GitHub.