Files
forge-steel/src/rand.c
T
2026-05-16 18:47:51 +02:00

24 lines
644 B
C

#include "rand.h"
static unsigned long long mcg_state = 0xcafef00dd15ea5e5u; // Must be odd
static unsigned long long const multiplier = 6364136223846793005u;
// https://en.wikipedia.org/wiki/Permuted_congruential_generator
static unsigned long fast_rand(void) {
unsigned long long x = mcg_state;
unsigned count = (unsigned)(x >> 61);
mcg_state = x * multiplier;
x ^= x >> 22;
return (unsigned long)(x >> (22 + count));
}
void rand_set_seed(unsigned long long seed) {
mcg_state = 2 * seed + 1;
(void)fast_rand();
}
unsigned int rand_uint(const unsigned int max) {
return max == 0 ? 0 : (unsigned int)(fast_rand() % max);
}