bchavez / bchavez/Bogus

The PickRandom method should be extended with a Minimum and Maximum amount as well as returning NULL when zero is the Minimum amount

Open
#577 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
9.7k
Forks
538
PR merge metrics
No merged PRs in 30d

Description

### Description

I want to be able to pass a collection to this method and have it return between X (minimum) and Y (maximum) items.
The method also accept an opt-in for returning NULL when the amount of picked items is zero.

### LINQPad Code Example

```csharp
var participantsEmpty = new Faker().PickRandom(people, 0, 5).ToList();
```
This would return between zero and five items from the people collection. The method will return an empty collection when the amount of items that will be returned is zero.

```csharp
var participantsNull = new Faker().PickRandom(people, 0, 5, true).ToList();
```
This would return between zero and five items from the people collection. As we supply **true** as the third parameter, the method will return NULL when the amount of items that will be returned is zero.

### What alternatives have you considered?

The following could be used as a workaround:
```csharp
var faker = new Faker();
var attendeeCount = faker.Random.Number(2, 8);
var participantsEmpty = faker.PickRandom(people, attendeeCount).ToList();
someObject.Participants = participantsEmpty.Count == 0 ? NULL : participantsEmpty
```

### Could you help with a pull-request?

Yes

Contributor guide

Open the contributing guide

Research direction

Start at the PickRandom entry point and its existing overloads, then compare the requested minimum, maximum, and null-on-zero behavior with the examples in the issue. Done means the collection can produce between the requested bounds and the zero-result option has the stated empty-collection or NULL behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.