Pcg32Engine Structure

Represents a PCG32 XSH-RR random number generator engine with 32-bit output.

Definition

Namespace: Numerics.NET.Random.Engines
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public struct Pcg32Engine : IScalarEngine<uint>, 
	IRandomEngine, IStateful, IAdvanceable, IJumpable
Inheritance
Object  →  ValueType  →  Pcg32Engine
Implements
IRandomEngine, IScalarEngine<UInt32>, IAdvanceable, IJumpable, IStateful

Remarks

The PCG32 XSH-RR (Xorshift-High, Random-Rotate) engine uses a 64-bit linear congruential generator (LCG) with multiplier 6364136223846793005. It features a 64-bit state with a period of 264 and provides excellent statistical quality while being extremely fast.

Algorithm Details:

  • State size: 64 bits (state + increment)
  • Output function: XSH-RR 64/32 (Xorshift-High, Random-Rotate)
  • Period: 264
  • Output ordering: Output-then-advance (outputs from state before advancing)

Compatibility:

This engine is compatible with the reference PCG32 implementation and provides the same output sequence for a given seed. It is widely used for its excellent balance of speed, quality, and memory efficiency.

Performance:

Extremely fast with minimal state (16 bytes). Ideal for applications requiring large numbers of independent streams or memory-constrained environments. Supports efficient advancement and jumping operations for parallel stream generation.

Reference: Melissa E. O'Neill. "PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation." Harvey Mudd College, 2014.

See also: https://www.pcg-random.org/

Example

C#
var engine = new Pcg32Engine();
engine.Seed(SeedSequences.SplitMix64(42));

uint value = engine.Next();

uint[] values = new uint[100];
engine.Fill(values);

byte[] bytes = new byte[1000];
engine.Fill(bytes.AsSpan());

Properties

BitsPerWord Gets the native word size in bits.
JumpSize Gets the size category of the primary jump operation.
LongJumpSize Gets the size category of the long jump operation.
Name Gets a human-readable name for this engine.
StateSize Gets the number of bytes required to save/load this engine's state.

Methods

Advance() Advances the engine state by one transition step.
Advance(UInt64) Advances the RNG state by the specified number of outputs.
EqualsIndicates whether this instance and a specified object are equal.
(Inherited from ValueType)
Fill Fills the destination span with successive native words from the engine's stream.
GetHashCodeReturns the hash code for this instance.
(Inherited from ValueType)
GetTypeGets the Type of the current instance.
(Inherited from Object)
Jump Jumps the RNG forward by the specified number of primary jump strides.
LoadState Loads the engine's state from the source span in a stable little-endian format.
LongJump Jumps the RNG forward by the specified number of long jump strides.
Next Advances the engine by one step and returns the native word produced for that step.
NextBounded Returns a random unsigned integer less than the specified value.
Output Returns the native word produced by applying the output mapping function to the current engine state, without mutating that state.
SaveState Saves the engine's state to the destination span in a stable little-endian format.
Seed(SeedSequence) Initializes the engine state from a seed sequence.
Seed(UInt64, UInt64) Directly sets the PCG32 state and stream.
ToStringReturns the fully qualified type name of this instance.
(Inherited from ValueType)

See Also