build: move map & rng logic to their own libraries

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I1802469f3baff4576f61accfb5a197d86a6a6964
This commit is contained in:
raf 2026-04-10 15:17:39 +03:00
commit ef0a20c1f8
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
11 changed files with 68 additions and 20 deletions

22
libs/rng/rng.c Normal file
View file

@ -0,0 +1,22 @@
#include "rng.h"
// Linear congruential generator (LCG) state
static unsigned int g_seed = 1;
// LCG parameters (from numerical recipes)
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_MOD 4294967294 // 2^32 - 2 (avoid 0)
void rng_seed(unsigned int seed) {
// Ensure seed is never 0
g_seed = (seed == 0) ? 1 : seed;
}
int rng_int(int min, int max) {
// Generate next value
g_seed = (LCG_A * g_seed + LCG_C) % LCG_MOD;
// Map to [min, max] range
return min + (int)((unsigned long long)g_seed % (max - min + 1));
}

10
libs/rng/rng.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef RNG_H
#define RNG_H
// Seed the RNG with a deterministic value
void rng_seed(unsigned int seed);
// Get a random integer in range [min, max]
int rng_int(int min, int max);
#endif // RNG_H