kesac/Syllabore

Inject random generator

tmx7777 opened this issue · 3 comments

I propose that all classes that use System.Random should provide a constructor overload that allows a Random instance to be injected by the caller.

This would allow the caller to create a random generator with a specific seed. This is helpful when creating test data that is always the same based on that seed. For example that would be the case, if one uses NUnit's random generator which is derived fromRandom and seeded individually for each test case.

Alternatively it could be injected as Func<int> or as an interface.

kesac commented

I agree, this will be useful. Random instantiation occurs in the constructors of NameGenerator, SyllableGenerator, TransformerSet, and DefaultNameTransformer which isn't a lot to overload.

There is also a static class, ListExtensions, that keeps a static Random instance for its extension methods. I'll look at getting rid of it and reworking the extension methods so there can be more control over Random.

kesac commented

New changes in upcoming version v2.3.3:

  • Generator classes that simulate randomness now implement IRandomizable
  • The instance of System.Random within generator classes is now accessible through the Random property
    • This affects NameGenerator, SyllableGenerator, SyllableSet, and TransformSet
  • In addition, a new fluent-style method has been added to each generator class to set the Random instance during configuration
  • The wiki page here has been added and contains code examples on controlling random seeds (through the property or through the fluent methods)

I ended up skipping adding more constructor overloads, but not against it in the future if controlling seeds becomes a common activity.

kesac commented

v2.3.3 is now published on NuGet.org and GitHub