From 54062e0ad62457e8bec161a6192d4d6894c521f1 Mon Sep 17 00:00:00 2001 From: nhmall Date: Mon, 28 Jan 2019 19:54:18 -0500 Subject: [PATCH] include the rnd.c bits --- src/rnd.c | 79 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/src/rnd.c b/src/rnd.c index 551f33637..fa8ce0e20 100644 --- a/src/rnd.c +++ b/src/rnd.c @@ -7,28 +7,71 @@ #ifdef USE_ISAAC64 #include "isaac64.h" +#if 0 static isaac64_ctx rng_state; +#endif + +struct rnglist_t { + int FDECL((*fn), (int)); + boolean init; + isaac64_ctx rng_state; +}; + +enum {CORE = 0, DISP}; + +static struct rnglist_t rnglist[] = { + {rn2, FALSE, {0}}, /* CORE */ + {rn2_on_display_rng, FALSE, {0}}, /* DISP */ +}; + +int +whichrng(fn) +int (*fn)(int); +{ + int i; + + for (i = 0; i < SIZE(rnglist); ++i) + if (rnglist[i].fn == fn) + return i; + return -1; +} void -init_isaac64(unsigned long seed) +init_isaac64(seed, fn) +unsigned long seed; +int FDECL((*fn),(int)); { unsigned char new_rng_state[sizeof(seed)]; - int i; + int i, rngindx = whichrng(fn); + + if (rngindx < 0) + panic("Bad rng function passed to init_isaac64()."); + for (i=0; i>= 8; } - - isaac64_init(&rng_state, new_rng_state, sizeof(seed)); + isaac64_init(&rnglist[rngindx].rng_state, new_rng_state, sizeof(seed)); } static int RND(int x) { - return (isaac64_next_uint64(&rng_state) % x); + return (isaac64_next_uint64(&rnglist[CORE].rng_state) % x); } -#else +/* 0 <= rn2(x) < x, but on a different sequence from the "main" rn2; + used in cases where the answer doesn't affect gameplay and we don't + want to give users easy control over the main RNG sequence. */ +int +rn2_on_display_rng(x) +register int x; +{ + return (isaac64_next_uint64(&rnglist[DISP].rng_state) % x); +} + +#else /* USE_ISAAC64 */ + /* "Rand()"s definition is determined by [OS]conf.h */ #if defined(LINT) && defined(UNIX) /* rand() is long... */ extern int NDECL(rand); @@ -41,7 +84,15 @@ extern int NDECL(rand); #define RND(x) ((int) ((Rand() >> 3) % (x))) #endif #endif /* LINT */ -#endif +int +rn2_on_display_rng(x) +register int x; +{ + static unsigned seed = 1; + seed *= 2739110765; + return (int)((seed >> 16) % (unsigned)x); +} +#endif /* USE_ISAAC64 */ /* 0 <= rn2(x) < x */ int @@ -60,20 +111,6 @@ register int x; #endif } -/* 0 <= rn2(x) < x, but on a different sequence from the "main" rn2; - used in cases where the answer doesn't affect gameplay and we don't - want to give users easy control over the main RNG sequence. This is - an intentionally low-quality RNG to discourage its use for anything - gameplay-affecting; please use a better RNG in that case. */ -int -rn2_on_display_rng(x) -register int x; -{ - static unsigned seed = 1; - seed *= 2739110765; - return (int)((seed >> 16) % (unsigned)x); -} - /* 0 <= rnl(x) < x; sometimes subtracting Luck; good luck approaches 0, bad luck approaches (x-1) */ int -- 2.40.0