Skip to main content

IRandomSystem

IRandomSystem wraps the two non-deterministic primitives most code reaches for: Random and Guid.

What it covers

Sub-propertyWraps
Randomnew Random() and Random.Shared - including a thread-safe shared instance under .NET Framework
GuidGuid.NewGuid() and the related factories

Implementations

TypePackageUse it for
RealRandomSystemTestably.AbstractionsProduction
MockRandomSystemTestably.Abstractions.TestingUnit tests
IRandomSystem randomSystem = new MockRandomSystem();

int n = randomSystem.Random.Shared.Next(100);
Guid id = randomSystem.Guid.NewGuid();

By default the mock seeds itself fresh per test and produces unpredictable values - useful for catching code that accidentally depends on a specific output. See Deterministic randomness for how to drive Random and Guid from a known sequence.