Comments (2)
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:
- First, "keeping an exclusion list" in memory potentially leads to multiple invocations of the
random
generator. That is, multiple invocations of therandom
generator "for another value" in a single method call can make deterministic sequences hard to debug in large complex generations or object graphs. - Second, we open up a potential big
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). - Lastly, what happens when we accidentally run
NumberWithExclusion(1, 3, except: listOfExclusions);
we get stuck an infinite loop whenlistOfExclusions = [1,2,3]
. It seems obvious right now, but whenexcept:
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.
from bogus.
many thanks for the detailed response. Will look at adding this extension to our code where we need it.
from bogus.
Related Issues (20)
- Faker.Tv() is missing from Bogus.Hollywood HOT 3
- Feature request: New localized data: Word(s).
- Update Denmark (da) locale from faker-js
- Nuget for .NET 8.0.x HOT 4
- Random history facts generation
- Are Randomizer's number ranges inclusive or exclusive? HOT 3
- bogus is missing NuGet package README file HOT 1
- .NET 8 support HOT 9
- Gender-digit is incorrect for Swedish Personnummer generation HOT 3
- Switch to licence expression rather than URL HOT 1
- Invalid Iban generation for CR (Costa Rica) HOT 3
- Add hungarian (hu) locale from faker-js HOT 2
- How to make convert to generic method HOT 6
- Fakers used inside Rules of other Fakers share the same object reference HOT 2
- Determinism with Custom Datasets HOT 1
- Include the increment count for the Rules Expressions HOT 1
- Stack memory issue in locale tests (overflows on Mac) HOT 2
- Update `zh_CN` dataset to latest HOT 3
- private setters workaround is not working for generic types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bogus.