{"record":{"id":"fd9088b592ff49d4","repo":"bchavez/Bogus","slug":"nameof-amounttopick-is-greater-than-the-number","errorCode":null,"errorMessage":"{nameof(amountToPick)} is greater than the number of items.","messagePattern":"(.+?) is greater than the number of items\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Faker.cs","lineNumber":298,"sourceCode":"   {\n      return this.Random.ArrayElement(items);\n   }\n\n   /// <summary>\n   /// Helper to pick random subset of elements out of the list.\n   /// </summary>\n   /// <param name=\"amountToPick\">amount of elements to pick of the list.</param>\n   /// <exception cref=\"ArgumentException\">if amountToPick is lower than zero.</exception>\n   public IEnumerable<T> PickRandom<T>(IEnumerable<T> items, int amountToPick)\n   {\n      if( amountToPick < 0 )\n      {\n         throw new ArgumentOutOfRangeException($\"{nameof(amountToPick)} needs to be a positive integer.\");\n      }\n      var size = items.Count();\n      if( amountToPick > size )\n      {\n         throw new ArgumentOutOfRangeException($\"{nameof(amountToPick)} is greater than the number of items.\");\n      }\n      return this.Random.Shuffle(items).Take(amountToPick);\n   }\n\n   /// <summary>\n   /// Helper method to call faker actions multiple times and return the result as IList of T\n   /// </summary>\n   public IList<T> Make<T>(int count, Func<T> action)\n   {\n      return Enumerable.Range(1, count).Select(n => action()).ToList();\n   }\n\n   /// <summary>\n   /// Helper method to call faker actions multiple times and return the result as IList of T.\n   /// This method passes in the current index of the generation.\n   /// </summary>\n   public IList<T> Make<T>(int count, Func<int, T> action)\n   {","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Faker.cs#L280-L316","documentation":"Faker.PickRandom(items, amountToPick) throws ArgumentOutOfRangeException when amountToPick exceeds the number of items in the sequence, because Shuffle+Take cannot return more elements than exist.","triggerScenarios":"Calling f.PickRandom(items, 10) when items has fewer than 10 elements; calling with amountToPick larger than a filtered/empty source.","commonSituations":"Assuming a source list is large enough when it is empty or small; computing amountToPick from a page size larger than the data set.","solutions":["Clamp amountToPick to items.Count() (materialize once if enumerating multiple times).","Validate the source is non-empty and large enough before picking.","Use f.Random.Shuffle(items).Take(Math.Min(amountToPick, items.Count())) explicitly."],"exampleFix":"// before\nvar subset = f.PickRandom(items, 10);\n\n// after\nvar list = items as IList<T> ?? items.ToList();\nvar subset = f.PickRandom(list, Math.Min(10, list.Count));","handlingStrategy":"validation","validationCode":"var list = items as IList<T> ?? items.ToList();\nint amountToPick = Math.Clamp(requestedCount, 0, list.Count);\nvar subset = f.PickRandom(list, amountToPick);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Materialize the source once if you need its count more than once.","Clamp amountToPick to the actual item count.","Handle empty sources explicitly before picking."],"tags":["faker","argument-validation","collections"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}