From 08cb1349ca394634c4e1df6cec423428e6b29ad6 Mon Sep 17 00:00:00 2001 From: copilot-swe-agent[bot] Date: Fri, 20 Feb 2026 07:04:47 +0000 Subject: Add XorShift, SplitMix64, WyRandom, PCG, and XoroShiro generators Co-authored-by: 8e8m <248551607+8e8m@users.noreply.github.com> --- source/pcg_random.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 source/pcg_random.h (limited to 'source/pcg_random.h') diff --git a/source/pcg_random.h b/source/pcg_random.h new file mode 100644 index 0000000..ae4886a --- /dev/null +++ b/source/pcg_random.h @@ -0,0 +1,52 @@ +/* MIT License - Copyright (c) 2025 wallstop + * Original C# implementation from: https://github.com/wallstop/unity-helpers + * Converted to C for librandom project + */ + +#ifndef PCG_RANDOM_H +#define PCG_RANDOM_H + +#include +#include + +/* PCG (Permuted Congruential Generator): High-quality, small-state PRNG. + * + * PCG offers excellent statistical quality with very small state and extremely + * fast generation. This implementation uses a 64-bit state with 32-bit outputs + * and an increment (stream selector). + * + * Pros: + * - Fast and allocation-free; suitable for gameplay hot paths + * - Great statistical quality; passes TestU01 BigCrush and PractRand + * - Deterministic and reproducible across platforms + * - Small state footprint + * + * Cons: + * - Not cryptographically secure + * - 32-bit outputs (use multiple calls for 64-bit) + */ + +typedef struct { + uint64_t state; + uint64_t increment; +} pcg_random_t; + +/* Initialize the generator with a seed */ +void pcg_init(pcg_random_t *rng, uint64_t seed); + +/* Initialize with explicit state and increment */ +void pcg_init_full(pcg_random_t *rng, uint64_t state, uint64_t increment); + +/* Generate next random 32-bit unsigned integer */ +uint32_t pcg_next_uint(pcg_random_t *rng); + +/* Generate next random 64-bit unsigned integer */ +uint64_t pcg_next_ulong(pcg_random_t *rng); + +/* Generate next random float in [0, 1) */ +float pcg_next_float(pcg_random_t *rng); + +/* Generate next random double in [0, 1) */ +double pcg_next_double(pcg_random_t *rng); + +#endif /* PCG_RANDOM_H */ -- cgit v1.2.3