aboutsummaryrefslogtreecommitdiff
path: root/source/pcg_random.h
blob: ae4886aaaa6cfe58c180004125312c98aafe3178 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 <stdint.h>
#include <stdbool.h>

/* 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 */