{"record":{"id":"e5f637ba3185d113","repo":"bchavez/Bogus","slug":"the-nameof-count-parameter-is-count-and-the-c","errorCode":null,"errorMessage":"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}.","messagePattern":"The (.+?) parameter is (.+?) and the calculated set of enums has a length of (.+?)\\. It is impossible to pick (.+?) enums from a list of (.+?)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":706,"sourceCode":"      T[] enums;\n      if( exclude.Length > 0)\n      {\n         enums = System.Enum.GetValues(typeof(T))\n            .OfType<T>()\n            .Except(exclude)\n            .ToArray();\n      }\n      else\n      {\n         enums = System.Enum.GetValues(typeof(T))\n            .OfType<T>()\n            .Except(exclude)\n            .ToArray();\n      }\n\n      if( count > enums.Length || count < 0 )\n      {\n         throw new ArgumentOutOfRangeException(nameof(count), count,\n         $\"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}.\");\n      }\n\n      return this.ArrayElements(enums, count);\n   }\n\n   /// <summary>\n   /// Shuffles an IEnumerable source.\n   /// </summary>\n   public IEnumerable<T> Shuffle<T>(IEnumerable<T> source)\n   {\n      List<T> buffer = source.ToList();\n      for( var i = 0; i < buffer.Count; i++ )\n      {\n         int j;\n         //lock any seed access, for thread safety.\n         lock( Locker.Value )\n         {","sourceCodeStart":688,"sourceCodeEnd":724,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L688-L724","documentation":"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.","triggerScenarios":"Calling f.Random.EnumValues(count: 10) on an enum with only 3 members, or EnumValues(-1, ...), or excluding members so count > remaining length.","commonSituations":"Hardcoding a count larger than the enum size; computing count from unrelated config; excluding members then requesting more than remain.","solutions":["Compute the available pool first and clamp count: count = Math.Min(count, pool.Length).","Ensure count >= 0 and <= number of non-excluded enum values.","Pass null for count to let Bogus pick a random amount."],"exampleFix":"// before\nvar vals = f.Random.EnumValues<MyEnum>(count: 10);\n// after\nint pool = Enum.GetValues<MyEnum>().Length;\nvar vals = f.Random.EnumValues<MyEnum>(count: Math.Min(10, pool));","handlingStrategy":"validation","validationCode":"static T[] SafeEnumValues<T>(Bogus.Randomizer r, int? count, params T[] exclude) where T : struct, Enum {\n    int pool = System.Enum.GetValues<T>().Except(exclude).Count();\n    int? safe = count is int c ? Math.Clamp(c, 0, pool) : null;\n    return r.EnumValues(safe, exclude);\n}","typeGuard":null,"tryCatchPattern":"try { return r.EnumValues(count, exclude); }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"impossible to pick\")) {\n    return r.EnumValues(null, exclude); // let Bogus pick a random count\n}","preventionTips":["Clamp count to the non-excluded enum size.","Pass null for count when a random amount is acceptable.","Compute exclude-aware pool size before requesting count."],"tags":["bogus","randomizer","enumvalues","count-validation"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}