NumpySeedSequence Class

Implements NumPy's SeedSequence algorithm for cross-language reproducibility with Python.

Definition

Namespace: Numerics.NET.Random.Seeding
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public sealed class NumpySeedSequence : SeedSequence
Inheritance
Object  →  SeedSequence  →  NumpySeedSequence

Remarks

NumpySeedSequence provides byte-for-byte compatibility with NumPy's numpy.random.SeedSequence class, enabling deterministic seeding that produces identical results across Python (NumPy) and .NET when using the same seed values and stream identifiers.

Algorithm:

The algorithm uses a pool-based mixing design derived from Melissa O'Neill's improved seed_seq design. Input entropy is absorbed into a fixed-size pool (default 4 words) using hash mixing operations with constants INIT_A/MULT_A and INIT_B/MULT_B. The pool is then mixed thoroughly to ensure all bits affect all other bits. Entropy generation cycles through the pool, applying per-position mixing to produce output values.

Pool-Based State:

Unlike SplitMix64-based sequences, NumPy's algorithm maintains a pool of 32-bit words (default size: 4 words) that represents the fully mixed entropy. Output generation cycles through this pool, applying position-dependent mixing to each value. The pool size is fixed at construction and determines the period before cycling.

Usage:

This sequence is selected by Numpy. For explicitly supported generators (PCG64, PCG64-DXSM, SFC64, Philox), it provides full NumPy state compatibility. For other generators, it provides NumPy-compatible entropy expansion.

Reference:

NumPy documentation: https://numpy.org/doc/stable/reference/random/bit_generators/ generated/numpy.random.SeedSequence.html

Melissa O'Neill's PCG paper (Appendix on seeding): https://www.pcg-random.org/paper.html

  Note

This is a purely 32-bit algorithm matching NumPy's implementation. 64-bit values are formed by packing consecutive 32-bit values in little-endian order, consistent with NumPy's behavior when generating uint64 state.

Constructors

NumpySeedSequence(ReadOnlySpan<UInt32>, StreamAddress) Initializes a new instance of the NumpySeedSequence class with 32-bit entropy values and optional stream address.
NumpySeedSequence(ReadOnlySpan<UInt64>, StreamAddress) Initializes a new instance of the NumpySeedSequence class with 32-bit entropy values and optional stream address.
NumpySeedSequence(UInt64, StreamAddress) Initializes a new instance of the NumpySeedSequence class with a 64-bit seed and optional stream address.

Properties

PoolSize Gets the number of 32-bit words in the entropy pool.

Methods

EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Fill(Span<UInt32>) Fills a span of 32-bit unsigned integers with entropy values.
(Overrides SeedSequence.Fill(Span<UInt32>))
Fill(Span<UInt64>) Fills a span of 64-bit unsigned integers with entropy values.
(Overrides SeedSequence.Fill(Span<UInt64>))
Fill(UInt64[]) Fills an array of 64-bit unsigned integers with entropy values.
(Inherited from SeedSequence)
FillBytes Fills a span of bytes with entropy values.
(Inherited from SeedSequence)
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetTypeGets the Type of the current instance.
(Inherited from Object)
NextUInt32 Generates the next 32-bit unsigned integer from the entropy stream.
(Overrides SeedSequence.NextUInt32())
NextUInt64 Generates the next 64-bit unsigned integer from the entropy stream.
(Inherited from SeedSequence)
Reset Resets the internal state of the entropy stream to the initial state.
(Inherited from SeedSequence)
ToStringReturns a string that represents the current object.
(Inherited from Object)

Fields

DefaultPoolSize The default pool size used by NumPy's SeedSequence.

See Also