Wy Rand Engine Structure
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public struct WyRandEngine : IScalarEngine<ulong>,
IRandomEngine, IStateful, IAdvanceable- 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
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
| Bits | Gets the native word size in bits. |
| Name | Gets a human-readable name for this engine. |
| State | Gets the number of bytes required to save/load this engine's state. |
Methods
| Advance() | Advances the engine state by one transition step. |
| Advance( | Advances the RNG state by the specified number of outputs. |
| Equals | Indicates 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. |
| Get | Returns the hash code for this instance. (Inherited from ValueType) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Load | 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. |
| Save | Saves the engine's state to the destination span in a stable little-endian format. |
| Seed( | Initializes the engine state from a seed sequence. |
| Seed( | Directly sets the internal 64-bit state. |
| ToString | Returns the fully qualified type name of this instance. (Inherited from ValueType) |