Closed acnicholls-kroll closed 7 months ago
Hi @acnicholls-kroll, this is a bit of a tricky request;
The reason Bogus doesn't support this out of the box is that this scenario can present a few problems:
random
generator. That is, multiple invocations of the random
generator "for another value" in a single method call can make deterministic sequences hard to debug in large complex generations or object graphs.O(runtime)
scaling problem when the exclusion list starts to grow. What matching algorithm do we use to scan for values? binary search? or quick array scan? Additionally, as the exclusion list grows, we slow down (and performance problems like this are hard to diagnose).NumberWithExclusion(1, 3, except: listOfExclusions);
we get stuck an infinite loop when listOfExclusions = [1,2,3]
. It seems obvious right now, but when except:
is a dynamically generated/derived exclusion list because of database removals, we start leading the developer down a bad path of potentially getting their code in a stuck state.For example, to create a custom extension method for the Randomizer
instance:
void Main()
{
var faker = new Faker();
var result = faker.Random.NumberWithExclusion(1,4, except: [1, 2, 3]);
result.Dump();
}
public static class MyBogusExtensions
{
public static int NumberWithExclusion(this Randomizer random, int min, int max, int[] except)
{
int result = 0;
do
{
result = random.Number(min, max);
}
while( except.Contains(result) );
return result;
}
}
Notice, we need to re-generate the random.Number()
when the generated value is in exclusion. And this kind of code inside Bogus makes it extremely difficult to debug a deterministic sequence because we could potentially pull more values from the seeded sequence rather than just one time.
So in summary, I try to avoid adding "try again" code in Bogus because it makes deterministic debugging more difficult.
But nothing prevents you from adding the extension method above to your code base.
I'll keep it in mind for future enhancement (if I continue to get more requests like this), but for now, I'll try to avoid adding code like this to the main codebase.
many thanks for the detailed response. Will look at adding this extension to our code where we need it.
Please describe why you are requesting a feature
we have a lookups table where the PK is the integer value with a string field. Some records have been removed, meaning the PK values are not 100% sequential. When using randomized data from BOGUS, we sometimes cannot insert that data into a related table with an FK field because the random number is one where the corresponding PK value is missing.
Can you create an extension that would allow us to designate values that would not be preferred within the range of valid random values?
Please provide a code example of what you are trying to achieve
Please answer any or all of the questions below
Is the feature something that currently cannot be done? Not sure how to affect this without re-generating the model if an "offending" value is selected
What alternatives have you considered? re-generating the model or value if an offending value is created.
Has the feature been requested in the past? not to my knowledge
If the feature request is approved, would you be willing to submit a PR?
Yes