CounterBasedRandomSource<TGenerator, TWord> Class

Base class for counter-based random number generators using Layer 2 generators.

Definition

Namespace: Numerics.NET.Random
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public abstract class CounterBasedRandomSource<TGenerator, TWord> : RandomSource<TGenerator>, 
	ICounterBased<TWord>
where TGenerator : struct, new(), IBlockRandomGenerator, ICounterBased<TWord>
where TWord : struct, new(), Object
Inheritance
Object  →  RandomSource<TGenerator>  →  CounterBasedRandomSource<TGenerator, TWord>
Derived
Implements
ICounterBased<TWord>

Type Parameters

TGenerator
The underlying generator type of the random source, used to enable optimizations like inlining. Must be a struct implementing IRandomGenerator and ICounterBased<T>.
TWord
The native word type (typically UInt32 or UInt64).

Remarks

CounterBasedRandomSource<TGenerator, TWord> extends RandomSource<TGenerator> to add counter-based RNG (CBRNG) functionality. It implements ICounterBased<T> by delegating all counter operations to the underlying Layer 2 generator.

Counter-Based Architecture:

Counter-based RNGs use a cryptographic-like structure where output is computed from a counter and key using a deterministic function. This design enables:

  • Efficient parallel generation: Multiple threads can generate independent subsequences by using different counter ranges.
  • Fast skip-ahead: Jumping to arbitrary positions in the sequence is O(1) by setting the counter directly.
  • Block generation: Output is computed in fixed-size blocks, where each block corresponds to one counter value.
  • Reproducibility: Setting the key and counter to specific values always produces the same output block.

Key Counter-Based Methods:

This class exposes counter-based operations through the ICounterBased<T> interface:

Consistency Protocol:

After calling SetKey(ReadOnlySpan<TWord>), SetCounter(ReadOnlySpan<TWord>), or ResetCounter(), the generator enters an inconsistent state. Callers must invoke GenerateBlock() before using output methods to bring the generator back to a consistent state.

Example Derived Class:

C#
public sealed class Philox4x64 
    : CounterBasedRandomSource&lt;BlockGenerator64<Philox4x64Engine>, ulong>
{
    public Philox4x64() : base() { }

    public Philox4x64(ulong seed, StreamAddress streamAddress = default, SeedProfile seedProfile = SeedProfile.Default)
        : base(seed, streamAddress, seedProfile) { }

    protected override void Initialize(in RandomOptions options, ref CounterBasedGenerator64<Philox4x64Engine> generator)
    {
        SeedSequence seedSequence = options.CreateSeedSequence();
        // Initialize generator using seed sequence
    }

    public override string Name => "Philox4x64";
    public override int BitsPerWord => 64;
}

Common Counter-Based Generators:

  • Philox (4x32, 4x64)
  • Threefry (4x64)
  • ChaCha (configurable rounds)

Constructors

CounterBasedRandomSource<TGenerator, TWord>() Constructs a new counter-based random number generator using a high-entropy, automatically generated seed.
CounterBasedRandomSource<TGenerator, TWord>(TGenerator) Constructs a new counter-based random number generator from a pre-initialized generator.
CounterBasedRandomSource<TGenerator, TWord>(RandomOptions) Constructs a new counter-based random number generator with advanced options.
CounterBasedRandomSource<TGenerator, TWord>(ReadOnlySpan<Byte>) Constructs a new counter-based random number generator from saved state.
CounterBasedRandomSource<TGenerator, TWord>(Int64, StreamAddress, SeedProfile) Constructs a new counter-based random number generator with the specified seed.
CounterBasedRandomSource<TGenerator, TWord>(ReadOnlySpan<TWord>, StreamAddress, SeedProfile) Constructs a new counter-based random number generator with a seed array.

Properties

BitsPerWord Gets the number of bits per word in the generator's native output.
(Inherited from RandomSource<TGenerator>)
BlockLength Gets the number of output words per block produced by this generator.
CounterLength Gets the number of words in the engine counter.
Generator Gets a reference to the underlying Layer 2 generator.
(Inherited from RandomSource<TGenerator>)
KeyLength Gets the number of words in the engine key.
Name Gets a human-readable name for this random number generator.
(Inherited from RandomSource<TGenerator>)
StateSize Gets the number of bytes required to save/load this generator's state.
(Inherited from RandomSource<TGenerator>)
Uncached Gets an uncached facade for this random number generator.
(Inherited from RandomSource<TGenerator>)

Methods

AdvanceBlock Advances the counter by the specified number of blocks.
EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Fill(Span<UInt32>) Fills a span with random 32-bit unsigned integer values.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int32>, Int32) Fills a span with random 32-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int64>, Int64) Fills a span with random 64-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int32>, Int32, Int32) Fills a span with random 32-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int64>, Int64, Int64) Fills a span with random 64-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GenerateBlock Computes the output block for the current key and counter.
GetCounter Copies the current counter into the provided destination span.
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetKey Copies the current key into the provided destination span.
GetState Returns the current state of the generator as a byte array.
(Inherited from RandomSource<TGenerator>)
GetTypeGets the Type of the current instance.
(Inherited from Object)
Initialize Initializes the generator based on the provided options.
(Inherited from RandomSource<TGenerator>)
LoadState Loads the generator's state from the source span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>)
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Next() Returns a non-negative random integer.
(Inherited from RandomSource<TGenerator>)
Next(Int32) Returns a non-negative random integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Next(Int32, Int32) Returns a random integer within a specified range.
(Inherited from RandomSource<TGenerator>)
NextBytes(Byte[]) Fills the elements of a specified array of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Span<Byte>) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Byte[], Boolean) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Span<Byte>, Boolean) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextDouble Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>)
NextInt64() Returns a non-negative random 64-bit integer.
(Inherited from RandomSource<TGenerator>)
NextInt64(Int64) Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
NextInt64(Int64, Int64) Returns a random 64-bit integer within a specified range.
(Inherited from RandomSource<TGenerator>)
NextSingle Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>)
NextUInt32 Returns a random 32-bit unsigned integer.
(Inherited from RandomSource<TGenerator>)
NextUInt64 Returns a random 64-bit unsigned integer.
(Inherited from RandomSource<TGenerator>)
Reinitialize Reinitializes this random number generator using the specified initialization options.
(Inherited from RandomSource<TGenerator>)
ResetCounter Resets the counter to all zeros.
SaveState Saves the generator's state to the destination span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>)
SetCounter Sets the counter to the specified value.
SetKey Sets the key to the specified value.
ToStringReturns a string that represents the current object.
(Inherited from Object)

See Also