GfsrEngine Structure

Represents a Generalized Feedback Shift Register (GFSR) 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 GfsrEngine : IScalarEngine<uint>, 
	IRandomEngine, IStateful
Inheritance
Object  →  ValueType  →  GfsrEngine
Implements
IRandomEngine, IScalarEngine<UInt32>, IStateful

Remarks

The GFSR engine is a linear feedback shift register-based pseudo-random number generator using a fourth-order generalized feedback shift register algorithm. It features a very large state array and an exceptionally long period.

Algorithm Details:

  • State size: 524288 bits (16384 × 32-bit words + index)
  • Period: 29689 - 1
  • Taps: Four-tap LFSR at positions 471, 1586, 6988, and 9689
  • Performance: Very fast generation after initialization

Characteristics:

GFSR generators are extremely simple and fast once initialized. The output at each step is computed by XORing four previous values at fixed lag positions. However, they have several drawbacks:

  • Large state: 64 KB of state memory
  • Slow initialization: Filling the large state array takes significant time
  • Poor quality: Fails many modern statistical tests (BigCrush)
  • Linear: Being a linear generator, it's unsuitable for cryptography

Historical Context:

GFSR was popular in the 1990s for scientific computing due to its simplicity and speed. However, it has been superseded by better algorithms like the Mersenne Twister and modern generators like Xoshiro/Xoroshiro.

Modern Alternatives:

For new applications, strongly prefer modern generators like Xoshiro256StarStarEngine or Pcg64Engine which offer far better statistical quality, much smaller state, and similar or better performance.

Note: This generator is provided for legacy compatibility and historical reproducibility only. It does not support jumping or fast-forward operations.

Reference: Robert M. Ziff, "Four-tap shift-register-sequence random-number generators." Computers in Physics 12(4), Jul/Aug 1998, pp 385-392.

Example

C#
var engine = new GfsrEngine();
engine.Seed(SeedSequences.SplitMix64(12345));

uint value = engine.Next();

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

// Save and restore state
byte[] state = new byte[engine.StateSize];
engine.SaveState(state);
engine.LoadState(state);

Constructors

GfsrEngine Initializes a new GFSR engine.

Properties

BitsPerWord Gets the native word size in bits.
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.
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)
LoadState Loads the engine's state from the source span in a stable little-endian format.
Next Advances the engine by one step and returns the native word produced for that step.
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(ReadOnlySpan<UInt32>) Directly sets the GFSR state array.
Seed(SeedSequence) Initializes the engine state from a seed sequence.
ToStringReturns the fully qualified type name of this instance.
(Inherited from ValueType)

See Also