Random Interop Extensions Class
Provides extension methods for interoperability between Numerics.NET random sources
and Random.
Definition
Namespace: Numerics.NET.Random
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public static class RandomInteropExtensions- Inheritance
- Object → RandomInteropExtensions
Remarks
RandomInteropExtensions enables seamless conversion between Numerics.NET's IRandomSource/RandomSource<TGenerator> and .NET's built-in Random at API boundaries. The extension methods in this class make it easy to use Numerics.NET random number generators with APIs that expect a Random instance, and vice versa.
Key Features:
- RandomSource → System.Random: AsRandom(IRandomSource) wraps random sources for APIs that expect a Random.
- System.Random → RandomSource: AsRandomSource(Random) wraps Random instances for use in APIs that expect random sources.
- Conservative round-tripping: TryUnwrap(Random, IRandomSource) and TryUnwrap<T>(Random, T) enable identity recovery without reflection or cloning.
Interoperability Boundary:
These methods exist primarily for interop, not performance:
- No caching guarantees: Repeated calls may allocate new wrappers. Users must treat these methods as adapter boundaries, not stable identities.
- No thread-safety improvements: Wrappers inherit thread-safety from the underlying RNG instance.
Round-tripping Behavior:
- rng.AsRandom().AsRandomSource() returns the original random source.
- sys.AsRandomSource().AsRandom() returns the original Random.
- TryUnwrap succeeds only for supported wrapper types, failing cleanly for incompatible types.
Methods
| AsRandom( | Converts a random source to a Random. |
| AsRandom<T>(T) | Converts a typed random source to a Random. |
| AsRandom | Converts a Random to a random source. |
| Try | Attempts to unwrap a Random to recover the underlying random source. |
| Try | Attempts to unwrap a Random to recover the underlying random source with the exact specified type. |