63 lines
1.9 KiB
C
63 lines
1.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>
|
|
#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;
|
|
|
|
/** 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(rand_state *state) {
|
|
*state = *state * 1664525u + 1013904223u;
|
|
return *state;
|
|
}
|
|
|
|
/** 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);
|
|
}
|
|
|
|
/**
|
|
* 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 */
|