IRandomGenerator Interface
Definition
Defines the minimal contract for a pluggable, non-cryptographic random-integer source used by shuffle, sampling, and randomized-collection helpers throughout the library.
public interface IRandomGenerator
- Extension Methods
Examples
// Substitute a deterministic generator in tests.
IRandomGenerator rng = new XorShiftRandom(seed: 42);
int dieRoll = rng.Next(6) + 1; // value in [1, 6]
Remarks
The interface exists as a seam between consumers (helpers such as IListExtensions.Shuffle and
IEnumerableExtensions.Randomize) and the underlying random source. By depending on the abstraction rather
than on a concrete implementation, callers can substitute a deterministic generator in tests, supply a thread-local
generator under contention, or plug in a domain-specific PRNG without touching the helpers.
Two implementations ship with the library: XorShiftRandom - a fast, lock-free xorshift generator
suitable for tight inner loops - and SystemRandomAdapter, which wraps Random for
callers that prefer the BCL distribution and seeding semantics. Either is appropriate for shuffle and sampling work;
neither is suitable for cryptographic use. Consumers that require cryptographic randomness should use
System.Security.Cryptography.RandomNumberGenerator directly.
Implementations are not required to be thread-safe unless documented otherwise. Helpers that consume an IRandomGenerator from multiple threads must either choose a thread-safe implementation or wrap access externally.
Methods
Next(int)
Returns a non-negative random integer that is less than the specified maximum.
int Next(int maxValue)
Parameters
maxValueintThe exclusive upper bound.
Returns
- int
A random integer in the range [0, maxValue).
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |