You don't strictly need to use a Random
singleton throughout your application. Most of the time (including in this case) using more than one instance is a sign of preferring convenience over "theoretical correctness", but there are legitimate cases where different program components use different RNGs by design so that the behavior of each component over the lifetime of the program is deterministic.
If you want to move to a singleton instance you should use RegisterType
to instruct the container that it should satisfy all requests for a Random
object with the same instance:
container.RegisterType<Random>(new ContainerControlledLifetimeManager());
This will not create a Random
instance on the spot; instead, it will instruct the container that once a request for Random
has been resolved, subsequent requests will be resolved with that same instance.
After that, it's just a matter of resolving the singleton instance with container.Resolve<Random>()
or making the dependency visible to the container so that it automatically resolves and injects the RNG instance when you instruct it to resolve one of the RNG's users.