Counter Based Random Source<TGenerator, TWord> Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
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:
- GetKey(Span<TWord>) / SetKey(ReadOnlySpan<TWord>): Access and modify the generator key
- GetCounter(Span<TWord>) / SetCounter(ReadOnlySpan<TWord>): Access and modify the counter
- ResetCounter(): Reset the counter to all zeros
- GenerateBlock(): Recompute the current output block
- AdvanceBlock(UInt64): Skip ahead by multiple blocks
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:
public sealed class Philox4x64
: CounterBasedRandomSource<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
| Counter | Constructs a new counter-based random number generator using a high-entropy, automatically generated seed. |
| Counter | Constructs a new counter-based random number generator from a pre-initialized generator. |
| Counter | Constructs a new counter-based random number generator with advanced options. |
| Counter | Constructs a new counter-based random number generator from saved state. |
| Counter | Constructs a new counter-based random number generator with the specified seed. |
| Counter | Constructs a new counter-based random number generator with a seed array. |
Properties
| Bits |
Gets the number of bits per word in the generator's native output.
(Inherited from RandomSource<TGenerator>) |
| Block | Gets the number of output words per block produced by this generator. |
| Counter | Gets the number of words in the engine counter. |
| Generator |
Gets a reference to the underlying Layer 2 generator.
(Inherited from RandomSource<TGenerator>) |
| Key | Gets the number of words in the engine key. |
| Name |
Gets a human-readable name for this random number generator.
(Inherited from RandomSource<TGenerator>) |
| State |
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
| Advance | Advances the counter by the specified number of blocks. |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
| Fill( |
Fills a span with random 32-bit unsigned integer values.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) |
| Generate | Computes the output block for the current key and counter. |
| Get | Copies the current counter into the provided destination span. |
| Get | Serves as the default hash function. (Inherited from Object) |
| Get | Copies the current key into the provided destination span. |
| Get |
Returns the current state of the generator as a byte array.
(Inherited from RandomSource<TGenerator>) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Initialize |
Initializes the generator based on the provided options.
(Inherited from RandomSource<TGenerator>) |
| Load |
Loads the generator's state from the source span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>) |
| Memberwise | Creates a shallow copy of the current Object. (Inherited from Object) |
| Next() |
Returns a non-negative random integer.
(Inherited from RandomSource<TGenerator>) |
| Next( |
Returns a non-negative random integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next( |
Returns a random integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified array of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a non-negative random 64-bit integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 64-bit integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 32-bit unsigned integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
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>) |
| Reset | Resets the counter to all zeros. |
| Save |
Saves the generator's state to the destination span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>) |
| Set | Sets the counter to the specified value. |
| Set | Sets the key to the specified value. |
| ToString | Returns a string that represents the current object. (Inherited from Object) |