{"record":{"id":"64e765edfbf2721e","repo":"bchavez/Bogus","slug":"nameof-amounttopick-needs-to-be-a-positive-inte","errorCode":null,"errorMessage":"{nameof(amountToPick)} needs to be a positive integer.","messagePattern":"(.+?) needs to be a positive integer\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Faker.cs","lineNumber":293,"sourceCode":"\n   /// <summary>\n   /// Picks a random item of T specified in the parameter list.\n   /// </summary>\n   public T PickRandomParam<T>(params T[] items)\n   {\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>","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Faker.cs#L275-L311","documentation":"Faker.PickRandom(items, amountToPick) rejects a negative amountToPick with ArgumentOutOfRangeException. Note the check is '< 0', so zero is accepted (and yields an empty result) even though the message says 'positive integer' - the wording is looser than the actual guard.","triggerScenarios":"Calling f.PickRandom(items, -1) or passing a negative count derived from user input or a calculation that underflows.","commonSituations":"Passing an unvalidated user-supplied count, or computing amountToPick as (desired - offset) where offset exceeds desired.","solutions":["Clamp amountToPick to a non-negative value before calling.","Validate the upstream input and reject negatives early with a clear error.","Remember 0 is allowed and returns an empty enumerable."],"exampleFix":"// before\nvar subset = f.PickRandom(items, requestedCount);\n\n// after\nvar subset = f.PickRandom(items, Math.Max(0, requestedCount));","handlingStrategy":"validation","validationCode":"int amountToPick = Math.Max(0, requestedCount);\nvar subset = f.PickRandom(items, amountToPick);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate user-supplied counts at the boundary.","Remember 0 is allowed and returns empty; negatives are not.","Clamp with Math.Max(0, value) when in doubt."],"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"}