CounterBasedGenerator64<TEngine> Structure

A generator struct that adapts a 64-bit counter-based engine into the IRandomGenerator interface, with uint caching for efficient 32-bit word generation.

Definition

Namespace: Numerics.NET.Random.Generators
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public struct CounterBasedGenerator64<TEngine> : IBlockRandomGenerator, 
	IRandomGenerator, IStateful, ICounterBased<ulong>, IAdvanceable
where TEngine : struct, new(), ICounterBased<ulong>, IBlockEngine<ulong>, IStateful
Inheritance
Object  →  ValueType  →  CounterBasedGenerator64<TEngine>
Implements
ICounterBased<UInt64>, IAdvanceable, IBlockRandomGenerator, IRandomGenerator, IStateful

Type Parameters

TEngine
The engine type, which must be a struct implementing ICounterBased<T> and IBlockEngine<T> for UInt64, and IStateful.

Remarks

CounterBasedGenerator64<TEngine> wraps a counter-based 64-bit engine (e.g., Philox-4x64) and provides:

Refill Policy (Advance-on-Empty, Consistent-by-Contract):

When the block is exhausted (_index == BlockLength), the generator calls engine.AdvanceBlock(1) which advances the counter by one block and establishes a consistent state for the new block coordinates. The generator then resets _index = 0. This ensures the engine is always in a consistent state when Output is called.

Consistency Guarantee:

All generator methods guarantee that whenever they produce output from the engine, the engine is in a consistent state. AdvanceBlock(UInt64) is guaranteed by Layer 1 to leave the engine consistent, so Layer 2 does not need to call GenerateBlock() in the normal refill path.

Uint Caching:

When NextUInt32() is called, if no cached value exists, the generator calls NextUInt64(), returns the low 32 bits, and caches the high 32 bits for the next call.

State Persistence:

Layer 2 state includes engine state (including any engine-owned cached block material), cursor index (4 bytes LE), cached uint (4 bytes LE), and cache flag (1 byte).

Constructors

CounterBasedGenerator64<TEngine> Initializes a new instance of the CounterBasedGenerator64<TEngine> struct with the specified engine.

Properties

BlockLength Gets the number of words per block produced by this generator.
CounterLength Gets the number of words in the engine counter.
KeyLength Gets the number of words in the engine key.
StateSize Gets the number of bytes required to save/load this engine's state.

Methods

Advance Advances the RNG state by the specified number of outputs.
AdvanceBlock Advances the counter by the specified number of blocks.
EqualsIndicates whether this instance and a specified object are equal.
(Inherited from ValueType)
Fill(Span<Double>) Fills the specified span with random double-precision floating-point numbers.
Fill(Span<Single>) Fills the specified span with random single-precision floating-point numbers.
Fill(Span<UInt32>) Fills the specified span with random 32-bit unsigned integers.
Fill(Span<UInt64>) Fills the specified span with random 64-bit unsigned integers.
GetCounter Copies the current counter into the provided destination span.
GetHashCodeReturns the hash code for this instance.
(Inherited from ValueType)
GetKey Copies the current key into the provided destination span.
GetTypeGets the Type of the current instance.
(Inherited from Object)
LoadState Loads the engine's state from the source span in a stable little-endian format.
NextBytes Fills the specified span with random bytes in little-endian order.
NextDouble Generates the next double-precision floating-point number.
NextSingle Generates the next single-precision floating-point number.
NextUInt32 Generates the next 32-bit unsigned integer.
NextUInt64 Generates the next 64-bit unsigned integer.
ResetCounter Resets the counter to all zeros.
SaveState Saves the engine's state to the destination span in a stable little-endian format.
SetCounter Sets the counter to the specified value.
SetKey Sets the key to the specified value.
ToStringReturns the fully qualified type name of this instance.
(Inherited from ValueType)

See Also