IRandomSource<TGenerator> Interface

Represents a random number generator with typed access to its underlying Layer 2 generator.

Definition

Namespace: Numerics.NET.Random
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public interface IRandomSource<TGenerator> : IRandomSource, 
	IStateful
where TGenerator : struct, new(), IRandomGenerator
Implements
IRandomSource, IStateful

Type Parameters

TGenerator
The underlying generator type of the random source, used to enable optimizations like inlining. Must be a struct implementing IRandomGenerator.

Remarks

IRandomSource<TGenerator> extends IRandomSource with typed access to the underlying Layer 2 generator struct. This enables:

  • Zero-overhead hot paths: Numerics.NET algorithms can obtain a ref to the generator once and operate directly on the struct, enabling full inlining and specialization by the JIT compiler.
  • No per-sample virtual dispatch: Hot-path operations avoid interface calls by working with concrete generator structs.
  • Generic constraint-based specialization: Algorithms can use where TRng : IRandomSource<TGenerator> to access typed generators while remaining algorithm-agnostic.

Usage Pattern:

Performance-critical Numerics.NET APIs should accept IRandomSource<TGenerator> and extract the generator reference immediately:

public static void FillUniform<TRng, TGenerator>(TRng rng, int maxValue, Span<int> values) where TRng : IRandomSource<TGenerator> where TGenerator : struct, IRandomGenerator { ref TGenerator gen = ref rng.Generator; // Now operate on gen directly for maximum performance }

Implementation Requirements:

  • The Generator property must return a ref to the stored generator instance. It must never return a copy.
  • All IRandomSource API methods should delegate to the generator to ensure consistent behavior.

Example

C#
// Pcg64 implements IRandomSource<ScalarGenerator64<Pcg64Engine>>
var rng = new Pcg64(42);

// Get typed access to the generator
ref var gen = ref rng.Generator;

// Now operate on gen directly
ulong value = gen.NextUInt64();

Properties

BitsPerWord Gets the number of bits per word in the generator's native output.
(Inherited from IRandomSource)
Generator Gets a reference to the underlying Layer 2 generator.
Name Gets a human-readable name for this random number generator.
(Inherited from IRandomSource)
StateSize Gets the number of bytes required to save/load this engine's state.
(Inherited from IStateful)

Methods

Fill(Span<Double>) Fills a span with random double-precision floating-point numbers in [0, 1).
(Inherited from IRandomSource)
Fill(Span<Single>) Fills a span with random single-precision floating-point numbers in [0, 1).
(Inherited from IRandomSource)
Fill(Span<UInt32>) Fills a span with random 32-bit unsigned integer values.
(Inherited from IRandomSource)
Fill(Span<UInt64>) Fills a span with random 64-bit unsigned integer values.
(Inherited from IRandomSource)
LoadState Loads the engine's state from the source span in a stable little-endian format.
(Inherited from IStateful)
Next() Returns a non-negative random integer.
(Inherited from IRandomSource)
Next(Int32) Returns a non-negative random integer less than the specified maximum.
(Inherited from IRandomSource)
Next(Int32, Int32) Returns a random integer within a specified range.
(Inherited from IRandomSource)
NextBytes(Byte[]) Fills the elements of a specified array of bytes with random numbers.
(Inherited from IRandomSource)
NextBytes(Span<Byte>) Fills the elements of a specified span of bytes with random numbers.
(Inherited from IRandomSource)
NextDouble Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from IRandomSource)
NextInt64() Returns a non-negative random 64-bit integer.
(Inherited from IRandomSource)
NextInt64(Int64) Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from IRandomSource)
NextInt64(Int64, Int64) Returns a random 64-bit integer within a specified range.
(Inherited from IRandomSource)
NextSingle Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from IRandomSource)
NextUInt32 Returns a random 32-bit unsigned integer.
(Inherited from IRandomSource)
NextUInt64 Returns a random 64-bit unsigned integer.
(Inherited from IRandomSource)
SaveState Saves the engine's state to the destination span in a stable little-endian format.
(Inherited from IStateful)

Extension Methods

AsRandom Converts a random source to a Random.
(Defined by RandomInteropExtensions)
CorrelatedSamples Generates a series of random variables with the specified correlation matrix.
(Defined by RandomSamplingExtensions)
CorrelatedSamples<TGenerator> Generates a series of random variables with the specified correlation matrix.
(Defined by RandomSamplingExtensions)
Fill Fills the elements of a specified span with random double-precision floating-point numbers.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random double-precision floating-point numbers.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random single-precision floating-point numbers.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random 32-bit signed integers.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random 64-bit signed integers.
(Defined by RandomSourceExtensions)
Fill Fills a span with random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Fill Fills a span with random samples from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Fill Fills the elements of a specified span with random 32-bit signed integers less than the specified maximum.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random 64-bit signed integers less than the specified maximum.
(Defined by RandomSourceExtensions)
Fill Fills a vector with random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Fill Fills the elements of a specified span with random 32-bit signed integers within a specified range.
(Defined by RandomSourceExtensions)
Fill Fills the elements of a specified span with random 64-bit signed integers within a specified range.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random double-precision floating-point numbers.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random single-precision floating-point numbers.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 32-bit signed integers.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 64-bit signed integers.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills a span with random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Fill<TGenerator> Fills a span with random samples from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 32-bit signed integers less than the specified maximum.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 64-bit signed integers less than the specified maximum.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills a vector with random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Fill<TGenerator> Fills a vector with random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 32-bit signed integers within a specified range.
(Defined by RandomSourceExtensions)
Fill<TGenerator> Fills the elements of a specified span with random 64-bit signed integers within a specified range.
(Defined by RandomSourceExtensions)
FillWithoutReplacement Populates the specified span with numbers chosen at random between 0 and the provided count without replacement.
(Defined by RandomSourceExtensions)
FillWithoutReplacement<TGenerator> Populates the specified span with numbers chosen at random between 0 and the provided count without replacement.
(Defined by RandomSourceExtensions)
Next Returns a non-negative random integer.
(Defined by RandomSourceExtensions)
Next Generates a random sample from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Next Generates a random sample from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Next Returns a non-negative random integer that is less than the specified maximum.
(Defined by RandomSourceExtensions)
Next Generates an array of random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Next Generates an array of random samples from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Next Returns a random integer that is within a specified range.
(Defined by RandomSourceExtensions)
Next<TGenerator> Returns a non-negative random integer.
(Defined by RandomSourceExtensions)
Next<TGenerator> Generates a random sample from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Next<TGenerator> Generates a random sample from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Next<TGenerator> Returns a non-negative random integer that is less than the specified maximum.
(Defined by RandomSourceExtensions)
Next<TGenerator> Generates an array of random samples from the specified continuous distribution.
(Defined by RandomSamplingExtensions)
Next<TGenerator> Generates an array of random samples from the specified discrete distribution.
(Defined by RandomSamplingExtensions)
Next<TGenerator> Returns a random integer that is within a specified range.
(Defined by RandomSourceExtensions)
NextDouble Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Defined by RandomSourceExtensions)
NextDouble<TGenerator> Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Defined by RandomSourceExtensions)
NextInt32 Returns a non-negative random 32-bit integer.
(Defined by RandomSourceExtensions)
NextInt32<TGenerator> Returns a non-negative random 32-bit integer.
(Defined by RandomSourceExtensions)
NextInt64 Returns a non-negative random 64-bit integer.
(Defined by RandomSourceExtensions)
NextInt64 Returns a non-negative random 64-bit integer that is less than the specified maximum.
(Defined by RandomSourceExtensions)
NextInt64 Returns a random 64-bit integer that is within a specified range.
(Defined by RandomSourceExtensions)
NextInt64<TGenerator> Returns a non-negative random 64-bit integer.
(Defined by RandomSourceExtensions)
NextInt64<TGenerator> Returns a non-negative random 64-bit integer that is less than the specified maximum.
(Defined by RandomSourceExtensions)
NextInt64<TGenerator> Returns a random 64-bit integer that is within a specified range.
(Defined by RandomSourceExtensions)
NextSingle Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Defined by RandomSourceExtensions)
NextSingle<TGenerator> Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Defined by RandomSourceExtensions)
Shuffle<T> Shuffles the elements of an IList<T> in place using the Fisher-Yates algorithm.
(Defined by RandomSourceExtensions)
Shuffle<T> Shuffles the elements of an array in place using the Fisher-Yates algorithm.
(Defined by RandomSourceExtensions)
Shuffle<T> Shuffles the elements of a span in place using the Fisher-Yates algorithm.
(Defined by RandomSourceExtensions)
Shuffle<TGenerator, T> Shuffles the elements of an array in place using the Fisher-Yates algorithm.
(Defined by RandomSourceExtensions)
Shuffle<TGenerator, T> Shuffles the elements of a span in place using the Fisher-Yates algorithm.
(Defined by RandomSourceExtensions)

See Also