Search code examples
c#unit-testingbogus

Can I specify the random seed for Bogus?


I'm using Bogus to generate test data.

Is there a way to set the seed it uses so it generates the same test data, in the same order?

For example, this test would fail:

var person1 = new Bogus.Person();
var person2 = new Bogus.Person();
Assert.AreEqual(person1.FullName, person2.FullName);

However, is there a way to reset the seed so that it wouldn't, ie:

Bogus.Config.SetSeed(1);
var person1 = new Bogus.Person();

Bogus.Config.SetSeed(1);
var person2 = new Bogus.Person();

Assert.AreEqual(person1.FullName, person2.FullName);

Solution

  • The readme has an example of this:

    //Set the randomzier seed if you wish to generate repeatable data sets.
    Randomizer.Seed = new Random(8675309);
    

    However, setting the seed means that the results of the random generator are consistent. To do what you want, you'll need to reset the seed before each call to get the same results.