Generation Module
The pyhdc.generation module provides deterministic random number
generators for reproducible hypervector generation.
HDCGenerator (abstract base)
- class pyhdc.generation.base.HDCGenerator(seed: int | None = None)[source]
Bases:
ABCAbstract base class for HDC-compatible random number generators.
Generators can produce bits, words, or floats, and are used to generate hypervectors with specific properties (e.g., using LFSRs, LCGs, etc.).
- generate_bits(length: int) List[int][source]
Generate a sequence of bits.
- Parameters:
length – Number of bits to generate
- Returns:
List of bits (0s and 1s)
- Raises:
NotImplementedError – If the generator doesn’t support bit generation
ValueError – If length is not positive
- generate_floats(length: int, min_val: float = -1.0, max_val: float = 1.0) List[float][source]
Generate a sequence of floating-point values.
- Parameters:
length – Number of floats to generate
min_val – Minimum value (inclusive)
max_val – Maximum value (inclusive)
- Returns:
List of floats in [min_val, max_val]
- Raises:
NotImplementedError – If the generator doesn’t support float generation
ValueError – If length is not positive
- generate_words(length: int, word_size: int = 32) List[int][source]
Generate a sequence of words.
- Parameters:
length – Number of words to generate
word_size – Size of each word in bits
- Returns:
List of words (integers)
- Raises:
NotImplementedError – If the generator doesn’t support word generation
ValueError – If length is not positive
- abstractmethod get_parameters() Dict[str, Any][source]
Get current generator parameters.
- Returns:
Dictionary of parameter names and values
- abstractmethod get_state() Any[source]
Get the current generator state.
- Returns:
The current state (generator-specific format)
- abstractmethod set_parameters(**kwargs) None[source]
Set generator parameters.
- Parameters:
**kwargs – Generator-specific parameters
- Raises:
ValueError – If parameters are invalid
Constructor:
HDCGenerator(seed=None)
- param seed:
Optional integer seed. If
None, the generator is initialised from a random source.
Abstract methods (must be implemented by subclasses):
_configure_internal(): called during__init__; sets up state from parameters_next_bit(): return the next bit (0 or 1); raiseNotImplementedErrorif not supported_next_word(word_size=32): return the next integer ofword_sizebitsset_parameters(**kwargs): update generator-specific parametersget_parameters(): return current parameter dictreset(): restore to initial state
Concrete methods:
generate_bits(length)→list[int]generate_words(length, word_size=32)→list[int]generate_floats(length, min_val=-1.0, max_val=1.0)→list[float]supports_bits()→boolsupports_words()→boolsupports_floats()→boolget_state()→ internal state (type is generator-dependent)set_seed(seed): set a new seed and reinitialise
DefaultGenerator
- class pyhdc.generation.base.DefaultGenerator(seed: int | None = None)[source]
Bases:
HDCGeneratorDefault generator using NumPy’s random number generator. Supports all output types.
NumPy-backed generator used when no custom generator is specified. Wraps
numpy.random.default_rng() internally.
- param seed:
Optional integer seed passed to
numpy.random.default_rng.
LCG family
- class pyhdc.generation.lcg.LCGGenerator(modulus: int = 4294967296, multiplier: int = 1664525, increment: int = 1013904223, seed: int | None = None)[source]
Bases:
HDCGeneratorLinear Congruential Generator for hypervector generation.
Uses the formula: X(n+1) = (a * X(n) + c) mod m where a is the multiplier, c is the increment, and m is the modulus.
- generate_floats(length: int, min_val: float = -1.0, max_val: float = 1.0) List[float][source]
Generate floats efficiently using LCG.
- set_parameters(modulus: int | None = None, multiplier: int | None = None, increment: int | None = None, seed: int | None = None) None[source]
Set LCG parameters.
- Parameters:
modulus – The modulus value (m)
multiplier – The multiplier value (a)
increment – The increment value (c)
seed – The seed value
Linear Congruential Generator: \(X_{n+1} = (a X_n + c) \bmod m\)
- param modulus:
m(default: 232)- param multiplier:
a(default: 1664525)- param increment:
c(default: 1013904223)- param seed:
Initial state
- class pyhdc.generation.lcg.MultiplicativeLCGGenerator(modulus: int = 2147483647, multiplier: int = 48271, seed: int | None = None)[source]
Bases:
LCGGeneratorMultiplicative LCG Generator (increment = 0).
Uses the formula: X(n+1) = (a * X(n)) mod m
LCG with c = 0 (multiplicative / Lehmer generator).
- class pyhdc.generation.CommonLCGGenerators[source]
Factory for named LCG presets. All methods take an optional
seedparameter and return anLCGGeneratorinstance.Method
Parameters
numerical_recipes(seed)a=1664525, c=1013904223, m=232
park_miller(seed)a=16807, c=0, m=231-1 (Lehmer)
minstd_rand(seed)a=48271, c=0, m=231-1
minstd_rand0(seed)a=16807, c=0, m=231-1
borland(seed)Borland C++ parameters
glibc(seed)GNU C Library parameters
msvc(seed)Microsoft Visual C++ parameters
randu(seed)IBM RANDU (historical; poor quality)
LFSR family
- class pyhdc.generation.lfsr.FibonacciLFSRGenerator(width: int = 32, taps: List[int] | None = None, seed: int | None = None)[source]
Bases:
LFSRGeneratorFibonacci-style LFSR generator.
DLFSR family
- class pyhdc.generation.dlfsr.FibonacciDLFSRGenerator(width: int = 32, word_size: int = 32, taps: List[int] | None = None, seed: int | None = None)[source]
Bases:
DLFSRGeneratorFibonacci-style DLFSR generator.
- class pyhdc.generation.dlfsr.GaloisDLFSRGenerator(width: int = 32, word_size: int = 32, taps: List[int] | None = None, seed: int | None = None)[source]
Bases:
DLFSRGeneratorGalois-style DLFSR generator.
- class pyhdc.generation.dlfsr.MatrixDLFSRGenerator(width: int = 32, word_size: int = 32, taps: List[int] | None = None, matrix_coeffs: List[int] | None = None, seed: int | None = None)[source]
Bases:
DLFSRGeneratorMatrix-based DLFSR generator.
Uses matrix multiplication over GF(2^word_size) for feedback.
LCA family
- class pyhdc.generation.lca.ElementaryLCAGenerator(width: int = 32, rule: int = 30, boundary: str = 'periodic', seed: int | None = None)[source]
Bases:
LCAGeneratorElementary LCA generator using standard elementary CA rules.
PCG family
- class pyhdc.generation.pcg.PCGGenerator(state_bits: int = 64, output_bits: int = 32, multiplier: int = 6364136223846793005, increment: int = 1442695040888963407, seed: int | None = None, permutation: str = 'xsh-rs')[source]
Bases:
HDCGeneratorPermuted Congruential Generator for hypervector generation.
Uses an LCG with a permutation function for improved statistical properties.
- generate_floats(length: int, min_val: float = -1.0, max_val: float = 1.0) List[float][source]
Generate floats efficiently using PCG.
- set_parameters(state_bits: int | None = None, output_bits: int | None = None, multiplier: int | None = None, increment: int | None = None, permutation: str | None = None, seed: int | None = None) None[source]
Set PCG parameters.
- Parameters:
state_bits – Number of bits in internal state
output_bits – Number of bits in output
multiplier – LCG multiplier value
increment – LCG increment value
permutation – Output permutation method
seed – The seed value
- class pyhdc.generation.pcg.MultiplicativePCGGenerator(state_bits: int = 64, output_bits: int = 32, multiplier: int = 6364136223846793005, seed: int | None = None)[source]
Bases:
PCGGeneratorPCG based on Multiplicative Congruential Generator.
Uses MCG (increment = 0) instead of LCG for the underlying generator.
Xorshift family
- class pyhdc.generation.xorshift.Xorshift32Generator(seed: int | None = None)[source]
Bases:
XorshiftGenerator32-bit Xorshift generator with (13,17,5) parameters.
- class pyhdc.generation.xorshift.Xorshift64Generator(seed: int | None = None)[source]
Bases:
XorshiftGenerator64-bit Xorshift generator with (13,7,17) parameters.
- class pyhdc.generation.xorshift.Xorshift128Generator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGenerator128-bit Xorshift generator (4 x 32-bit words).
- class pyhdc.generation.xorshift.XorshiftPlusGenerator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGeneratorXorshift+ generator with addition for improved low-bit randomness.
- class pyhdc.generation.xorshift.XorshiftStarGenerator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGeneratorXorshift* generator with multiplication for better statistics.
- class pyhdc.generation.xorshift.Xoroshiro128PlusGenerator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGeneratorXoroshiro128+ - fast variant with 128-bit state.
- class pyhdc.generation.xorshift.Xoroshiro128StarStarGenerator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGeneratorXoroshiro128** - improved statistics over Plus variant.
- class pyhdc.generation.xorshift.Xoshiro256StarStarGenerator(seed: int | List[int] | None = None)[source]
Bases:
XorshiftGeneratorXoshiro256** - modern xorshift with excellent properties.
- class pyhdc.generation.xorshift.SplitMix64Generator(seed: int | None = None)[source]
Bases:
XorshiftGeneratorSplitMix64 - good for seeding other generators.
- class pyhdc.generation.CommonXorshiftGenerators[source]
Method
Generator class
xorshift32(seed)Xorshift32Generator
xorshift64(seed)Xorshift64Generator
xorshift128(seed)Xorshift128Generator
- pyhdc.generation.xorshift.splitmix64_seed(seed)
Generate N independent seeds from a single master seed using the SplitMix64 algorithm. Useful for initialising multiple independent streams.
- Parameters:
seed – Master seed integer.
- Returns:
SplitMix64Generatorthat can be used to seed other generators.
ShiftedCounter family
- class pyhdc.generation.shifted_counter.FeistelCounterGenerator(bit_width: int = 32, shift_amount: int = 13, rounds: int = 4, round_keys: List[int] | None = None, seed: int | None = None)[source]
Bases:
ShiftedCounterGeneratorShifted counter with Feistel network mapping.
Uses a Feistel network to create an invertible permutation.
- class pyhdc.generation.shifted_counter.ARXCounterGenerator(bit_width: int = 32, shift_amount: int = 11, constants: List[int] | None = None, rotations: List[int] | None = None, seed: int | None = None)[source]
Bases:
ShiftedCounterGeneratorShifted counter with Addition-Rotation-XOR mapping.
Uses ARX operations for fast, secure mixing.
- class pyhdc.generation.shifted_counter.SPNCounterGenerator(bit_width: int = 32, shift_amount: int = 17, sbox_size: int = 4, sbox: List[int] | None = None, pbox: List[int] | None = None, seed: int | None = None)[source]
Bases:
ShiftedCounterGeneratorShifted counter with Substitution-Permutation Network mapping.
Uses S-boxes and permutation for invertible mapping.
- class pyhdc.generation.shifted_counter.CustomMappingCounterGenerator(bit_width: int = 32, shift_amount: int = 15, mapping_func: Callable[[int], int] | None = None, seed: int | None = None)[source]
Bases:
ShiftedCounterGeneratorShifted counter with custom user-defined mapping.
Allows users to provide their own invertible mapping function.