46 lines
1.1 KiB
C
46 lines
1.1 KiB
C
#include "prand.h"
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#define BUILD_SEED \
|
|
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * \
|
|
(uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
|
|
(uint64_t)(__TIME__[6]) * (uint64_t)(__TIME__[7]))
|
|
|
|
#define PRNG_SAVE_INTERVAL 50 // Save every 1000 calls to prand()
|
|
|
|
static uint64_t seed = BUILD_SEED;
|
|
|
|
uint64_t prand() {
|
|
seed = seed * 6364136223846793005ULL + 1;
|
|
return seed;
|
|
}
|
|
|
|
inline uint64_t prand_range(uint64_t min, uint64_t max) {
|
|
return min + (prand() % (max - min + 1));
|
|
}
|
|
|
|
void sprand(uint64_t s) {
|
|
if (s) {
|
|
seed = s;
|
|
} else {
|
|
rand_reseed();
|
|
}
|
|
}
|
|
|
|
void rand_reseed() { seed = BUILD_SEED; }
|
|
|
|
#define PRAND_MAIN
|
|
#ifdef PRAND_MAIN
|
|
int main() {
|
|
// time(NULL) provides a somewhat unique seed at runtime
|
|
sprand(time(NULL));
|
|
|
|
uint64_t rn1 = prand();
|
|
uint64_t rn2 = prand();
|
|
|
|
printf("rn1 = %lu\n", rn1);
|
|
printf("rn2 = %lu\n", rn2);
|
|
}
|
|
#endif
|