From: Holger Schemel Date: Wed, 5 Jan 2022 17:00:50 +0000 (+0100) Subject: added support for improved pseudo-random number generator X-Git-Tag: 4.3.0.3~6 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=00f5e0fc833b4600cf7cd8cac1a2f047e4d71285;p=rocksndiamonds.git added support for improved pseudo-random number generator --- diff --git a/src/libgame/misc.c b/src/libgame/misc.c index faf6c011..48d67a79 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -746,7 +746,7 @@ void SkipUntilDelayReached(unsigned int *counter_var, unsigned int delay, // random generator functions // ---------------------------------------------------------------------------- -unsigned int init_random_number(int nr, int seed) +static unsigned int init_random_number_ext(int nr, int seed) { if (seed == NEW_RANDOMIZE) { @@ -774,9 +774,32 @@ unsigned int init_random_number(int nr, int seed) return (unsigned int) seed; } +static unsigned int prng_seed_gettimeofday(void) +{ + struct timeval current_time; + + gettimeofday(¤t_time, NULL); + + prng_seed_bytes(¤t_time, sizeof(current_time)); + + return 0; +} + +unsigned int init_random_number(int nr, int seed) +{ + return (nr == RANDOM_BETTER ? prng_seed_gettimeofday() : + init_random_number_ext(nr, seed)); +} + +static unsigned int get_random_number_ext(int nr) +{ + return (nr == RANDOM_BETTER ? prng_get_uint() : + random_linux_libc(nr)); +} + unsigned int get_random_number(int nr, int max) { - return (max > 0 ? random_linux_libc(nr) % max : 0); + return (max > 0 ? get_random_number_ext(nr) % max : 0); } diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 27ae0f3d..19ac4be4 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -28,11 +28,14 @@ #define RANDOM_ENGINE 0 #define RANDOM_SIMPLE 1 +#define RANDOM_BETTER 2 #define InitEngineRandom(seed) init_random_number(RANDOM_ENGINE, seed) #define InitSimpleRandom(seed) init_random_number(RANDOM_SIMPLE, seed) +#define InitBetterRandom(seed) init_random_number(RANDOM_BETTER, seed) #define GetEngineRandom(max) get_random_number(RANDOM_ENGINE, max) #define GetSimpleRandom(max) get_random_number(RANDOM_SIMPLE, max) +#define GetBetterRandom(max) get_random_number(RANDOM_BETTER, max) // values for getFile...() and putFile...() #define BYTE_ORDER_BIG_ENDIAN 0 @@ -120,6 +123,7 @@ int log_2(unsigned int); boolean getTokenValueFromString(char *, char **, char **); +char *getUUIDExt(unsigned int (*function)(int)); char *getUUID(void); void InitCounter(void);