kesac / Syllabore

A fantasy name generator that does not use pre-made lists of names
MIT License
40 stars 2 forks source link

Inject random generator #14

Closed tmx7777 closed 5 months ago

tmx7777 commented 6 months ago

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 5 months ago

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 5 months ago

New changes in upcoming version v2.3.3:

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

kesac commented 5 months ago

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