Table of Contents

Class SFC64

Namespace
NumSharp
Assembly
NumSharp.dll

Chris Doty-Humphrey's Small Fast Chaotic PRNG (NumPy's numpy.random.SFC64): four 64-bit words of state, one of them a counter that guarantees a minimum period of 2**64 per seed.

public class SFC64 : BitGenerator
Inheritance
SFC64
Inherited Members

Remarks

Port of NumPy 2.4.2's _sfc64.pyx + src/sfc64/sfc64.{c,h}; the raw stream is byte-identical to NumPy's. Seeding: generate_state(3, uint64) fills s[0..2], the counter s[3] starts at 1, and twelve outputs are discarded (sfc64_set_seed). Like NumPy, SFC64 has no advance or jumped — use spawn(int) (or distinct seeds) for parallel streams.

Constructors

SFC64()

Constructs an SFC64 seeded from fresh, unpredictable OS entropy (NumPy's SFC64()).

public SFC64()

SFC64(ISeedSequence)

Constructs and seeds an SFC64 from the given seed sequence (generate_state(3, uint64) + sfc64_set_seed).

public SFC64(ISeedSequence seed)

Parameters

seed ISeedSequence

The seed sequence — normally a SeedSequence; any ISeedSequence works; null is NumPy's seed=None: a fresh OS-entropy SeedSequence (the parameter name is NumPy's, so seed: sequence ports verbatim).

Remarks

The overload a bare null literal binds (new SFC64(null), NumPy's SFC64(None)): the int[]/long[]/uint[] overloads are equally good targets for it under plain C# rules, so this one carries the higher OverloadResolutionPriority — harmless for every non-null argument, since only a seed sequence converts to ISeedSequence.

Exceptions

NotImplementedError

seed is a SeedlessSeedSequence.

SFC64(int[])

Constructs an SFC64 seeded from a sequence of non-negative integers through SeedSequence.

public SFC64(int[] seed)

Parameters

seed int[]

The seed words (each must be non-negative). Null is NumPy's None — fresh OS entropy (it used to seed the fixed empty-list stream).

Exceptions

ValueError

An element is negative.

SFC64(long)

Constructs an SFC64 seeded from a non-negative integer through SeedSequence.

public SFC64(long seed)

Parameters

seed long

The seed (must be non-negative).

Exceptions

ValueError

seed is negative.

SFC64(long[])

Constructs an SFC64 seeded from a sequence of non-negative integers through SeedSequence.

public SFC64(long[] seed)

Parameters

seed long[]

The seed words (each must be non-negative). Null is NumPy's None — fresh OS entropy (it used to seed the fixed empty-list stream).

Exceptions

ValueError

An element is negative.

SFC64(BigInteger)

Constructs an SFC64 seeded from an arbitrary-size non-negative integer through SeedSequence.

public SFC64(BigInteger seed)

Parameters

seed BigInteger

The seed (must be non-negative).

Exceptions

ValueError

seed is negative.

SFC64(uint[])

Constructs an SFC64 seeded from uint32 words through SeedSequence.

public SFC64(uint[] seed)

Parameters

seed uint[]

The seed words. Null is NumPy's None — fresh OS entropy (it used to seed the fixed empty-list stream).

SFC64(ulong)

Constructs an SFC64 seeded from a non-negative integer through SeedSequence.

public SFC64(ulong seed)

Parameters

seed ulong

The seed.

Properties

Name

The bit generator's class name, e.g. "PCG64". Drives Generator's repr and the state checks.

public override string Name { get; }

Property Value

string

Remarks

A subclass returns its own NumPy-style class name: Generator prints it as Generator(NAME). Its own state refusal (in SetStateCore(BitGeneratorState)) should name the same class, as the built-in generators' NumPy texts do.

state

Gets or sets the current internal state (NumPy's bit_generator.state), typed.

public SFC64.State state { get; set; }

Property Value

SFC64.State

Exceptions

TypeError

Setting null, or a state whose word array is unset — NumPy's broadcast of None into the uint64 words, int() argument must be a string, a bytes-like object or a real number, not 'NoneType'.

ValueError

The word array neither has 4 elements nor 1 (NumPy's broadcast error).

Methods

CreateFromSeed(ISeedSequence)

Constructs a new instance of this bit generator's concrete type seeded from seed (NumPy's type(self)(seed=seed)).

public override BitGenerator CreateFromSeed(ISeedSequence seed)

Parameters

seed ISeedSequence

The seed sequence.

Returns

BitGenerator

The new, independently seeded generator.

Remarks

spawn(int) builds every child through it, so a subclass returns its OWN type here - a subclass of a built-in generator that does not override it spawns instances of the built-in class.

FillDouble(double*, long)

Fills n next_double draws in [0, 1) (the stream NextDouble() yields).

public override void FillDouble(double* dst, long n)

Parameters

dst double*

The destination (at least n doubles).

n long

The number of draws (non-positive: nothing).

Remarks

The caller holds lock.

FillFloat(float*, long)

Fills n next_float draws in [0, 1) (the stream NextFloat() yields).

public override void FillFloat(float* dst, long n)

Parameters

dst float*

The destination (at least n floats).

n long

The number of draws (non-positive: nothing).

Remarks

The caller holds lock.

FillRaw(ulong*, long)

Fills n raw words (next_raw, widened to 64 bits) — the stream NextRaw() yields.

public override void FillRaw(ulong* dst, long n)

Parameters

dst ulong*

The destination (at least n words).

n long

The number of words (non-positive: nothing).

Remarks

The caller holds lock.

FillUInt32(uint*, long)

Fills n words of next_uint32, buffered halves included (the stream NextUInt32() yields).

public override void FillUInt32(uint* dst, long n)

Parameters

dst uint*

The destination (at least n words).

n long

The number of words (non-positive: nothing).

Remarks

The caller holds lock.

FillUInt64(ulong*, long)

Fills n words of next_uint64 (the stream NextUInt64() yields).

public override void FillUInt64(ulong* dst, long n)

Parameters

dst ulong*

The destination (at least n words; may be unaligned).

n long

The number of words (non-positive: nothing).

Remarks

The caller holds lock.

GetStateCore()

Snapshots the engine state.

public override BitGeneratorState GetStateCore()

Returns

BitGeneratorState

A fresh state object of the concrete generator's state type.

Remarks

The unlocked half of state's getter, which a subclass implements. Callers read state instead: called directly this skips lock, so a concurrent draw can tear the snapshot.

NextUInt32()

The next uniform 32-bit word (NumPy's next_uint32).

public override uint NextUInt32()

Returns

uint

A uniformly distributed 32-bit value; advances (or consumes a buffered half of) the engine state.

Remarks

Not synchronized, as NextUInt64(). A 64-bit engine returns the low half of a word and buffers the high half for the next call (NumPy's has_uint32 / uinteger), which its state must carry.

NextUInt64()

The next uniform 64-bit word (NumPy's next_uint64).

public override ulong NextUInt64()

Returns

ulong

A uniformly distributed 64-bit value; advances the engine state.

Remarks

Not synchronized: a caller outside Generator holds lock around its draws, or two threads tear the state and emit values of no seed's stream.

SetStateCore(BitGeneratorState)

Restores the engine state from a snapshot (validated against this generator's type).

public override void SetStateCore(BitGeneratorState value)

Parameters

value BitGeneratorState

The non-null snapshot.

Remarks

The unlocked half of state's setter, which a subclass implements (and which must refuse a snapshot of another type). Callers assign state instead: called directly this skips the null check and lock.

Exceptions

ValueError

The snapshot belongs to another bit generator type.