Coder Social home page Coder Social logo

Comments (2)

bchavez avatar bchavez commented on May 28, 2024

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 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.
  • 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 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.

from bogus.

acnicholls-kroll avatar acnicholls-kroll commented on May 28, 2024

many thanks for the detailed response. Will look at adding this extension to our code where we need it.

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.