fastrand/fastrand.h
2025-04-02 20:10:22 +02:00

154 lines
3.9 KiB
C

/* Single-header LCG random generator and faster-than-modulo rand_until, rand_between */
/* LICENCE: Feel free to use it but please mention MagosIT Kft or its youtube channel */
#ifndef FAST_RAND_H
#define FAST_RAND_H
#include <stdint.h>
#include <assert.h>
#ifndef NO_CSTDLIB
#include <stdlib.h>
#endif /* NO_CSTDLIB */
#ifdef __cplusplus
// C++-specific logic
#if defined(__GNUC__) || defined(__clang__)
#define restrict __restrict__ // GCC/Clang
#elif defined(_MSC_VER)
#define restrict __restrict // MSVC
#else
#error "Compiler not supported for 'restrict' keyword in C++"
#endif
#endif
/* Currently a single integer is enough */
typedef uint32_t rand_state;
/* Currently a single integer is enough */
struct rand_ilp_state {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint32_t f;
uint32_t g;
uint32_t h;
};
typedef struct rand_ilp_state rand_ilp_state;
/** Creates a random number generator state with given seed */
static inline rand_state init_rand_with(uint32_t seed) {
return seed;
}
static inline rand_ilp_state init_rand_ilp_with(
uint32_t seed1,
uint32_t seed2,
uint32_t seed3,
uint32_t seed4,
uint32_t seed5,
uint32_t seed6,
uint32_t seed7,
uint32_t seed8) {
rand_ilp_state ret;
ret.a = seed1;
ret.b = seed2;
ret.c = seed3;
ret.d = seed4;
ret.e = seed1;
ret.f = seed2;
ret.g = seed3;
ret.h = seed4;
return ret;
}
#ifndef NO_CSTDLIB
/** Creates a random number generator state with arc4random() which does not need seeding as it uses system etropy */
static inline rand_state init_rand() {
return arc4random();
}
/** Creates a random number generator state with arc4random() which does not need seeding as it uses system etropy */
static inline rand_ilp_state init_rand_ilp() {
rand_ilp_state ret;
ret.a = arc4random();
ret.b = arc4random();
ret.c = arc4random();
ret.d = arc4random();
ret.e = arc4random();
ret.f = arc4random();
ret.g = arc4random();
ret.h = arc4random();
return ret;
}
#endif /* NO_CSTDLIB */
// 32-bit LCG
static inline uint32_t lcg(rand_state *state) {
*state = *state * 1664525u + 1013904223u;
return *state;
}
#define RAND_ILP_MAX 7
enum RAND_ILP {
A = 0, B = 1, C = 2, D = 3,
E = 4, F = 5, G = 6, H = RAND_ILP_MAX
};
typedef enum RAND_ILP RAND_ILP;
// 32-bit LCG with more states - might be faster when called from a loop, see perf.cpp
static inline uint32_t lcg_ilp(rand_ilp_state *state, RAND_ILP which) {
if(which == A) {
state->a = state->a * 1664525u + 1013904223u;
return state->a;
} else if(which == B) {
state->b = state->b * 1664525u + 1013904223u;
return state->b;
} else if(which == C) {
state->c = state->c * 1664525u + 1013904223u;
return state->c;
} else if(which == D) {
state->d = state->d * 1664525u + 1013904223u;
return state->d;
} else if(which == E) {
state->e = state->e * 1664525u + 1013904223u;
return state->e;
} else if(which == F) {
state->f = state->f * 1664525u + 1013904223u;
return state->f;
} else if(which == G) {
state->g = state->g * 1664525u + 1013904223u;
return state->g;
} else if(which == H) {
state->h = state->h * 1664525u + 1013904223u;
return state->h;
}
assert(0);
}
/** Pick a "reasonably random" number in [0, until-1] without modulus */
static inline uint32_t rand_until(rand_state *restrict state, uint32_t until) {
uint32_t rand = lcg(state);
// Multiply by "until", take the upper 32 bits of the 64-bit result
return (uint32_t)(((uint64_t)rand * until) >> 32);
}
uint32_t fastmodlike(uint32_t num, uint32_t m) {
return (((uint64_t) num) * m);
}
/**
* Pick a "reasonably random" number in [from, to) without modulus.
*
* @param state The random state - See: init_rand() and init_rand_with(seed)
* @param from The smallest possible value
* @param to The biggest possible value + 1
* @returns A value in [from, to) interval
*/
static inline uint32_t rand_between(rand_state *restrict state, uint32_t from, uint32_t to) {
return from + rand_until(state, to - from);
}
#endif /* FAST_RAND_H */