bchavez/Bogus · error · ArgumentOutOfRangeException
{nameof(amountToPick)} is greater than the number of items.
Error message
{nameof(amountToPick)} is greater than the number of items. What it means
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.
Source
Thrown at Source/Bogus/Faker.cs:298
{
return this.Random.ArrayElement(items);
}
/// <summary>
/// Helper to pick random subset of elements out of the list.
/// </summary>
/// <param name="amountToPick">amount of elements to pick of the list.</param>
/// <exception cref="ArgumentException">if amountToPick is lower than zero.</exception>
public IEnumerable<T> PickRandom<T>(IEnumerable<T> items, int amountToPick)
{
if( amountToPick < 0 )
{
throw new ArgumentOutOfRangeException($"{nameof(amountToPick)} needs to be a positive integer.");
}
var size = items.Count();
if( amountToPick > size )
{
throw new ArgumentOutOfRangeException($"{nameof(amountToPick)} is greater than the number of items.");
}
return this.Random.Shuffle(items).Take(amountToPick);
}
/// <summary>
/// Helper method to call faker actions multiple times and return the result as IList of T
/// </summary>
public IList<T> Make<T>(int count, Func<T> action)
{
return Enumerable.Range(1, count).Select(n => action()).ToList();
}
/// <summary>
/// Helper method to call faker actions multiple times and return the result as IList of T.
/// This method passes in the current index of the generation.
/// </summary>
public IList<T> Make<T>(int count, Func<int, T> action)
{View on GitHub (pinned to 6ece18c5c2)
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.
Example fix
// before var subset = f.PickRandom(items, 10); // after var list = items as IList<T> ?? items.ToList(); var subset = f.PickRandom(list, Math.Min(10, list.Count));
Defensive patterns
Strategy: validation
Validate before calling
var list = items as IList<T> ?? items.ToList(); int amountToPick = Math.Clamp(requestedCount, 0, list.Count); var subset = f.PickRandom(list, amountToPick);
Prevention
- 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.
When it happens
Trigger: Calling f.PickRandom(items, 10) when items has fewer than 10 elements; calling with amountToPick larger than a filtered/empty source.
Common situations: Assuming a source list is large enough when it is empty or small; computing amountToPick from a page size larger than the data set.
Related errors
- {nameof(amountToPick)} needs to be a positive integer.
- The specified property '{property.Name}' does not have a set
- .{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' m
- .{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultW
- The locale '{locale}' does not exist. To see all available l
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/fd9088b592ff49d4.
Report an issue: GitHub.