RandomInteropExtensions 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#
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:

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(IRandomSource) Converts a random source to a Random.
AsRandom<T>(T) Converts a typed random source to a Random.
AsRandomSource Converts a Random to a random source.
TryUnwrap(Random, IRandomSource) Attempts to unwrap a Random to recover the underlying random source.
TryUnwrap<T>(Random, T) Attempts to unwrap a Random to recover the underlying random source with the exact specified type.

See Also