bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 494 forks source link

Is there a way to create a Faker object for primitive data types? #190

Closed vdurante closed 5 years ago

vdurante commented 5 years ago

I am trying to create e List of CPFs (brazilian IDs). I tried to do the following:

var cpfs = new Faker<string>().Rules((f, o) => { o = f.Person.Cpf(); }).Generate(5);

I guess internally, Bogus tries to instantiate a new string o and fails to do so. Is there a way to use Bogus to generate list of primitive types?

Thanks in advance.

bchavez commented 5 years ago

Hi Vitor,

Thanks for your question. The purpose of Faker<T> is to be used with Domain Objects, DTOs, or other User Type classes. T is not really for primitive or built-in types like string or int. So let's scratch that option.

To generate a list of CPFs, we'd have to use something like this:

var cpfs = Enumerable.Range(0,5)
                     .Select(i => new Person().Cpf())
                     .ToList();

cpfs.Dump();
OUTPUT:
386.629.435-27
507.474.505-52
512.237.960-27
571.073.842-52
943.659.996-05

Hope that helps!

Thanks, Brian

:beach_umbrella: :trumpet: Beach Boys - Good Vibrations (Nick Warren bootleg)

vdurante commented 5 years ago

@bchavez thank you! That is pretty elegant!

Kapusch commented 3 years ago

Just in case somebody still needs something to generate a collection of primitive data type :

var fakeBrands = new Faker<string>()
     .CustomInstantiator(f => f.Company.CompanyName());
Giorgi commented 3 months ago

Any workaround for creating List of ints or other primitive types?

bchavez commented 3 months ago

@Giorgi, to generate a list of primitive types:

void Main()
{
   var f = new Faker();

   var values = Enumerable.Range(1, 50)
                          .Select(_ => f.Random.UInt())
                          .ToList();

   values.Dump();
}

image

Faker<T> is generally for class types; for everyting else, Faker facade is recommended.

Giorgi commented 3 months ago

Thanks, is Faker threadsafe?

bchavez commented 3 months ago

Thanks, is Faker threadsafe?

It depends. There is a basic lock around System.Random when drawing numbers per Faker instance. Much of the "thread safety" is also inherited from System.Random:

You basically lose determinism if two or more threads are operating on the same Faker instance because OS thread scheduling is non-deterministic.

If you need to generate a large amount of data it is best to partition the data generation range per thread; eg

where each thread has its own instance of Faker and has a dedicated seed value. The technique is briefly described here:

Giorgi commented 1 month ago

@bchavez Is there any recommended way for generating BigInteger?

bchavez commented 1 month ago

Hi @Giorgi, there's no standard or recommended way for generating BigInteger types. It is whatever is based on your needs. Here are a few examples:

void Main()
{
   var faker = new Faker();
   faker.Random.BigInteger().Dump();
   faker.Random.BigIntegerDigits(80).Dump();
}

public static class ExtensionsForRandomizer
{
   public static BigInteger BigInteger(this Randomizer r)
   {
      var data = r.Bytes(30);
      var b = new BigInteger(data);
      return b;
   }
   public static BigInteger BigIntegerDigits(this Randomizer r, int digits)
   {
      var data = r.String2(digits, "0123456789");
      var b = System.Numerics.BigInteger.Parse(data);
      return b;
   }
}
OUTPUT:
-22308249314910248646346984522781668288108116497977709677600309392141845
41351867314669609175092923064604245036485730585133764699088587903417011361966885