WyRandEngine Structure

Represents a WyRand random number generator engine with 64-bit output.

Definition

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

Remarks

The WyRand engine is an extremely fast hash-based pseudo-random number generator by Wang Yi. It is based on the WyHash algorithm and provides excellent statistical properties with minimal computational overhead.

Algorithm Details:

  • State size: 64 bits (1 × 64-bit word)
  • Output function: WyHash mixing using 128-bit multiplication
  • Period: Approximately 264
  • Performance: Extremely fast, among the fastest high-quality generators

Statistical Quality:

WyRand passes all statistical tests including BigCrush and PractRand. The hash-based mixing provides excellent distribution and decorrelation between consecutive outputs.

Use Cases:

Particularly suitable for simulations, games, and applications where speed is critical. The simple state structure and minimal overhead make it ideal for high-throughput random number generation. Not cryptographically secure.

Advancement:

WyRand supports efficient O(1) advancement via IAdvanceable. The state transition is a simple addition, allowing instant forward jumps.

State Requirement:

WyRand requires a non-zero state. Zero state is invalid and will be rejected during LoadState(ReadOnlySpan<Byte>).

Reference: Wang Yi. "The fastest strong pseudo random number generator." https://github.com/wangyi-fudan/wyhash

Example

C#
var engine = new WyRandEngine();
engine.Seed(SeedSequences.SplitMix64(999));

ulong value = engine.Next();

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

// Advance by 1000 steps in O(1) time
engine.Advance(1000);

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.
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)
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(SeedSequence) Initializes the engine state from a seed sequence.
Seed(UInt64) Directly sets the internal 64-bit state.
ToStringReturns the fully qualified type name of this instance.
(Inherited from ValueType)

See Also