fastrand/fastrand.h

52 lines
1.6 KiB
C
Raw Normal View History

2025-04-01 19:21:39 +02:00
/* 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>
#ifndef NO_CSTDLIB
#include <stdlib.h>
#endif /* NO_CSTDLIB */
/* Currently a single integer is enough */
typedef uint32_t rand_state;
/** Creates a random number generator state with given seed */
static inline rand_state init_rand_with(uint32_t seed) {
return seed;
}
#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();
}
#endif /* NO_CSTDLIB */
// 32-bit LCG
static inline uint32_t lcg(uint32_t *state) {
*state = *state * 1664525u + 1013904223u;
return *state;
}
/** Pick a "reasonably random" number in [0, until-1] without modulus */
static inline uint32_t rand_until(uint32_t *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);
}
/**
* 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(uint32_t *state, uint32_t from, uint32_t to) {
return from + rand_until(state, to - from);
}
#endif /* FAST_RAND_H */