From: Markus Armbruster Date: Tue, 14 Aug 2012 17:23:12 +0000 (+0200) Subject: Encapsulate direct use of random(), srandom() in chance.c X-Git-Tag: v4.3.31~91 X-Git-Url: http://git.pond.sub.org/?p=empserver;a=commitdiff_plain;h=866859e9124e2a05544340058bc611de50bbf88d Encapsulate direct use of random(), srandom() in chance.c Wrap roll0() around random(), and seed_prng() around srandom(). In preparation of replacing the PRNG. --- diff --git a/include/chance.h b/include/chance.h index 194dcf02a..da5d18852 100644 --- a/include/chance.h +++ b/include/chance.h @@ -34,7 +34,9 @@ #define CHANCE_H extern int chance(double); +extern int roll0(int); extern int roll(int); extern int roundavg(double); +extern void seed_prng(unsigned); #endif diff --git a/include/damage.h b/include/damage.h index 3ef3a334c..09861f1a4 100644 --- a/include/damage.h +++ b/include/damage.h @@ -29,6 +29,7 @@ * Known contributors to this file: * Ken Stevens, 1995 * Steve McClure, 1998 + * Markus Armbruster, 2006-2012 */ #ifndef DAMAGE_H @@ -38,11 +39,11 @@ #define DPERCENT_DAMAGE(x) (100.0 * (x) / ((x) + 100.0)) #define DMINE_HITCHANCE(x) ((x) / ((x) + 20.0)) #define DMINE_LHITCHANCE(x) ((x) / ((x) + 35.0)) -#define MINE_DAMAGE() (22 + random()%21) -#define MINE_LDAMAGE() (11 + random()%20) +#define MINE_DAMAGE() (22 + roll0(21)) +#define MINE_LDAMAGE() (11 + roll0(20)) #define DTORP_HITCHANCE(range, vis) \ (0.9 / ((range)+1) + (((vis) < 6) ? (5-(vis)) * 0.03 : 0.0)) -#define TORP_DAMAGE() (torpedo_damage + (random() % torpedo_damage) + \ - (random() % torpedo_damage)) +#define TORP_DAMAGE() (torpedo_damage + roll0(torpedo_damage) + \ + roll0(torpedo_damage)) #endif diff --git a/src/lib/commands/anti.c b/src/lib/commands/anti.c index 174b1dd2b..7c5ac1bed 100644 --- a/src/lib/commands/anti.c +++ b/src/lib/commands/anti.c @@ -29,6 +29,7 @@ * Known contributors to this file: * Pat Loney, 1992 * Steve McClure, 1997 + * Markus Armbruster, 2004-2012 */ #include @@ -123,7 +124,7 @@ anti(void) sect.sct_mobil = 0; } sect.sct_loyal = sect.sct_loyal * 0.5; - n_cheleft = (random() % 4); + n_cheleft = roll0(4); /* 75% chance some che will get left */ if (n_cheleft) { /* Ok, now leave anywhere from 16% to 25% of the che */ diff --git a/src/lib/commands/laun.c b/src/lib/commands/laun.c index 3078782a1..4f226be99 100644 --- a/src/lib/commands/laun.c +++ b/src/lib/commands/laun.c @@ -331,7 +331,7 @@ launch_sat(struct plnstr *pp) } i = pp->pln_tech + pp->pln_effic; if (chance(1.0 - (i / (i + 50.0)))) { - dir = DIR_FIRST + random() % 6; + dir = DIR_FIRST + roll0(6); sx = xnorm(sx + diroff[dir][0]); sy = ynorm(sy + diroff[dir][1]); pr("Your trajectory was a little off.\n"); diff --git a/src/lib/commands/news.c b/src/lib/commands/news.c index 8fa0b2a6e..2ec5ef645 100644 --- a/src/lib/commands/news.c +++ b/src/lib/commands/news.c @@ -27,11 +27,12 @@ * news.c: Show current Empire news * * Known contributors to this file: - * + * Markus Armbruster, 2006-2012 */ #include +#include "chance.h" #include "commands.h" #include "news.h" #include "optlist.h" @@ -186,7 +187,7 @@ preport(struct nwsstr *np) /* * vary the order of the printing of "%d times " */ - if ((random() & 3) == 0 && np->nws_ntm > 1) { + if (roll0(4) == 0 && np->nws_ntm > 1) { sprintf(cp, "%s times ", ptr); cp += strlen(cp); np->nws_ntm = 1; @@ -194,7 +195,7 @@ preport(struct nwsstr *np) strcpy(cp, cname(np->nws_ano)); cp += strlen(cp); *cp++ = ' '; - sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS], + sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[roll0(NUM_RPTS)], cname(np->nws_vno)); cp += strlen(cp); if (np->nws_ntm != 1) { diff --git a/src/lib/gen/chance.c b/src/lib/gen/chance.c index 284680b1b..1738ab280 100644 --- a/src/lib/gen/chance.c +++ b/src/lib/gen/chance.c @@ -24,7 +24,7 @@ * * --- * - * chance.c: return 1 if "roll" is under the chance. + * chance.c: Roll dice * * Known contributors to this file: * @@ -36,12 +36,27 @@ #include #include "chance.h" +/* + * Return non-zero with probability D. + */ int chance(double d) { return d > (random() % 32768) / 32768.0; } +/* + * Return a random number in [0..N-1]. + */ +int +roll0(int n) +{ + return random() % n; +} + +/* + * Return a random number in [1..N]. + */ int roll(int n) { @@ -49,8 +64,8 @@ roll(int n) } /* - * round value to nearest int (on the average). E.g. rounds up - * with a chance proportional to the size of the fractional part. + * Round VAL to nearest integer (on the average). + * VAL's fractional part is chance to round up. */ int roundavg(double val) @@ -58,3 +73,14 @@ roundavg(double val) double flr = floor(val); return (int)(flr + chance(val - flr)); } + +/* + * Seed the pseudo-random number generator with SEED. + * The sequence of pseudo-random numbers is repeatable by seeding it + * with the same value. + */ +void +seed_prng(unsigned seed) +{ + srandom(seed); +} diff --git a/src/lib/subs/attsub.c b/src/lib/subs/attsub.c index 292c71f75..2d5a169cb 100644 --- a/src/lib/subs/attsub.c +++ b/src/lib/subs/attsub.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Ken Stevens, 1995 * Steve McClure, 1996-2000 - * Markus Armbruster, 2006-2011 + * Markus Armbruster, 2006-2012 */ #include @@ -1837,12 +1837,12 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist, * since a single dead guy normally wouldn't cause a commander to * rethink his strategies, but 50 dead guys might. */ - odds += (random() % 11 - 5) / 100.0; + odds += (roll0(11) - 5) / 100.0; if (odds < 0.0) odds = 0.1; if (odds > 1.0) odds = 1.0; - recalctime = 8 + (random() % 43); + recalctime = 8 + roll0(43); while (!success && ototal) { if (chance(odds)) { pr("!"); @@ -1858,9 +1858,9 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist, if (((a_cas + d_cas) % 70) == 69) pr("\n"); if (recalctime-- <= 0) { - recalctime = 8 + (random() % 43); + recalctime = 8 + roll0(43); odds = att_calcodds(ototal, dtotal); - odds += (random() % 11 - 5) / 100.0; + odds += (roll0(11) - 5) / 100.0; if (odds < 0.0) odds = 0.1; if (odds > 1.0) diff --git a/src/lib/subs/damage.c b/src/lib/subs/damage.c index 4c0f8f9c1..0005d7dbf 100644 --- a/src/lib/subs/damage.c +++ b/src/lib/subs/damage.c @@ -29,6 +29,7 @@ * Known contributors to this file: * Dave Pare, 1989 * Steve McClure, 1997 + * Markus Armbruster, 2004-2012 */ #include @@ -169,7 +170,7 @@ damage(int amt, int pct) return 0; tmp = amt * pct; lost = tmp / 100; - if (random() % 100 < tmp % 100) + if (roll0(100) < tmp % 100) lost++; return amt - lost; } diff --git a/src/lib/subs/landgun.c b/src/lib/subs/landgun.c index fc6f36079..e7bafa4ee 100644 --- a/src/lib/subs/landgun.c +++ b/src/lib/subs/landgun.c @@ -27,7 +27,7 @@ * landgun.c: Fire weapons * * Known contributors to this file: - * Markus Armbruster, 2006-2009 + * Markus Armbruster, 2006-2012 */ #include @@ -48,7 +48,7 @@ fortgun(int effic, int guns) double d; double g = MIN(guns, 7); - d = (random() % 30 + 20.0) * (g / 7.0); + d = (roll0(30) + 20.0) * (g / 7.0); d *= effic / 100.0; return d; } @@ -60,7 +60,7 @@ seagun(int effic, int guns) d = 0.0; while (guns--) - d += 10.0 + random() % 6; + d += 10.0 + roll0(6); d *= effic * 0.01; return d; } @@ -72,7 +72,7 @@ landunitgun(int effic, int guns) d = 0.0; while (guns--) - d += 5.0 + random() % 6; + d += 5.0 + roll0(6); d *= effic * 0.01; return d; } @@ -221,9 +221,9 @@ lnd_sabo(struct lndstr *lp, short item[]) lp->lnd_item[I_SHELL]--; dam = fortgun(3 * lp->lnd_effic, 7); if (item[I_SHELL] > 20) - dam += seagun(lp->lnd_effic, random() % (item[I_SHELL] / 10)); + dam += seagun(lp->lnd_effic, roll0(item[I_SHELL] / 10)); if (item[I_PETROL] > 100) - dam += seagun(lp->lnd_effic, random() % (item[I_PETROL] / 50)); + dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50)); return dam; } diff --git a/src/lib/subs/takeover.c b/src/lib/subs/takeover.c index 3a2ec8ead..12dae13bc 100644 --- a/src/lib/subs/takeover.c +++ b/src/lib/subs/takeover.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Dave Pare, 1986 * Steve McClure, 1996-2000 - * Markus Armbruster, 2007-2010 + * Markus Armbruster, 2007-2012 */ #include @@ -100,7 +100,7 @@ takeover(struct sctstr *sp, natid newown) if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic)))) continue; } - n = lp->lnd_effic - (30 + (random() % 100)); + n = lp->lnd_effic - (30 + roll0(100)); if (n < 0) n = 0; lp->lnd_effic = n; @@ -129,7 +129,7 @@ takeover(struct sctstr *sp, natid newown) * how spunky are these guys? * n: random number from -25:75 + (50 - loyalty) */ - n = (50 - sp->sct_loyal) + ((random() % 100) - 25); + n = (50 - sp->sct_loyal) + (roll0(100) - 25); if (n > 0 && sp->sct_own == sp->sct_oldown) { che_count = (civ * n / 3000) + 5; if (che_count * 2 > civ) @@ -186,7 +186,7 @@ takeover_plane(struct plnstr *pp, natid newown) * XXX If this was done right, planes could escape, * flying to a nearby friendly airport. */ - n = pp->pln_effic - (30 + (random() % 100)); + n = pp->pln_effic - (30 + roll0(100)); if (n < 0) n = 0; pp->pln_effic = n; diff --git a/src/lib/update/human.c b/src/lib/update/human.c index 56d0a8fd6..10b8d4600 100644 --- a/src/lib/update/human.c +++ b/src/lib/update/human.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Dave Pare, 1986 * Steve McClure, 1996 - * Markus Armbruster, 2004-2010 + * Markus Armbruster, 2004-2012 */ #include @@ -95,12 +95,12 @@ do_feed(struct sctstr *sp, struct natstr *np, short *vec, nreport(sp->sct_own, N_DIE_FAMINE, 0, 1); } sp->sct_work = 0; - sp->sct_loyal += (random() % 8) + 2; + sp->sct_loyal += roll0(8) + 2; } sctwork = 0; } else { if (sp->sct_work < 100) - sctwork = sp->sct_work + 8 + (random() % 15); + sctwork = sp->sct_work + 8 + roll0(15); if (sctwork > 100) sctwork = 100; if (!player->simulation) diff --git a/src/lib/update/plague.c b/src/lib/update/plague.c index 0cfbfe371..df179e0b9 100644 --- a/src/lib/update/plague.c +++ b/src/lib/update/plague.c @@ -28,6 +28,7 @@ * * Known contributors to this file: * Steve McClure, 1998-2000 + * Markus Armbruster, 2004-2012 */ #include @@ -182,7 +183,7 @@ plague_people(struct natstr *np, short *vec, } if (*ptime <= 0) { *pstage -= 1; - *ptime = etus / 2 + random() % etus; + *ptime = etus / 2 + roll0(etus); } return stage; } diff --git a/src/lib/update/populace.c b/src/lib/update/populace.c index e87da3ce8..4347a448d 100644 --- a/src/lib/update/populace.c +++ b/src/lib/update/populace.c @@ -28,6 +28,7 @@ * * Known contributors to this file: * Dave Pare, 1986 + * Markus Armbruster, 2004-2012 */ #include @@ -72,7 +73,7 @@ populace(struct natstr *np, struct sctstr *sp, int etu) n = roundavg(etu * 0.125); if (n == 0) n = 1; - n = sp->sct_loyal + (random() % n) + 1; + n = sp->sct_loyal + roll0(n) + 1; if (n > 127) n = 127; sp->sct_loyal = n; @@ -80,7 +81,7 @@ populace(struct natstr *np, struct sctstr *sp, int etu) if (sp->sct_loyal > 65 && mil < civ / 20) { int work_red; - work_red = sp->sct_loyal - (50 + (random() % 15)); + work_red = sp->sct_loyal - (50 + roll0(15)); n = sp->sct_work - work_red; if (n < 0) n = 0; diff --git a/src/lib/update/revolt.c b/src/lib/update/revolt.c index 4818f1d94..dbf2766e6 100644 --- a/src/lib/update/revolt.c +++ b/src/lib/update/revolt.c @@ -29,7 +29,7 @@ * Known contributors to this file: * Dave Pare, 1986 * Steve McClure, 1997-2000 - * Markus Armbruster, 2004-2011 + * Markus Armbruster, 2004-2012 */ #include @@ -66,7 +66,7 @@ revolt(struct sctstr *sp) return; che_uw = 0; /* che due to civilian unrest */ - n = 10 - (random() % 20); + n = 10 - roll0(20); che_civ = 3 + (civ * n / 500); if (che_civ < 0) che_civ = 0; @@ -77,7 +77,7 @@ revolt(struct sctstr *sp) che += che_civ; if (che < CHE_MAX) { /* che due to uw unrest */ - n = 10 + (random() % 30); + n = 10 + roll0(30); che_uw = 5 + (uw * n / 500); if (che_uw > uw) che_uw = uw; @@ -240,7 +240,7 @@ guerrilla(struct sctstr *sp) } if (mil > 0) { /* military won. */ - n = sp->sct_loyal - (random() % 15); + n = sp->sct_loyal - roll0(15); if (n < 0) n = 0; sp->sct_loyal = n; @@ -254,7 +254,7 @@ guerrilla(struct sctstr *sp) * guerrillas have to resort to blowing things up. * Note this disrupts work in the sector. */ - n = (random() % 10) + (random() % che); + n = roll0(10) + roll0(che); if (n > 100) n = 100; tmp = sp->sct_work - n; @@ -327,14 +327,14 @@ guerrilla(struct sctstr *sp) /* loyalty drops during recruitment efforts */ n = sp->sct_loyal; if (n < 30) - n += (random() % 5) + 1; + n += roll0(5) + 1; else if (n < 70) - n += (random() % 10) + 4; + n += roll0(10) + 4; if (n > 127) n = 127; sp->sct_loyal = n; if (sp->sct_oldown != sp->sct_own || n > 100) { - n = civ * (random() % 3) / 200; + n = civ * roll0(3) / 200; n /= hap_fact(tnat, getnatp(sp->sct_oldown)); if (n + che > CHE_MAX) n = CHE_MAX - che; @@ -342,7 +342,7 @@ guerrilla(struct sctstr *sp) civ -= n; sp->sct_item[I_CIVIL] = civ; } - n = uw * (random() % 3) / 200; + n = uw * roll0(3) / 200; if (n + che > CHE_MAX) n = CHE_MAX - che; che += n; @@ -375,7 +375,7 @@ domove: val = roundintby(val, 10); /* inject a modicum of indeterminism; also * avoids che preferring certain directions */ - val += random() % 10 - 5; + val += roll0(10) - 5; if (val >= min_mil) continue; nicest_sp = nsp; diff --git a/src/server/main.c b/src/server/main.c index 6a28d5c9f..9cb2761dd 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -50,6 +50,7 @@ #include "sys/socket.h" #endif +#include "chance.h" #include "empio.h" #include "empthread.h" #include "file.h" @@ -349,7 +350,7 @@ crash_dump(void) static void init_server(unsigned seed, int force_bad_state) { - srandom(seed); + seed_prng(seed); #if defined(_WIN32) loc_NTInit(); #endif diff --git a/src/server/update.c b/src/server/update.c index 21bda9cb9..51a571b99 100644 --- a/src/server/update.c +++ b/src/server/update.c @@ -30,7 +30,7 @@ * Dave Pare, 1994 * Steve McClure, 1996 * Ron Koenderink, 2005 - * Markus Armbruster, 2007-2010 + * Markus Armbruster, 2007-2012 */ #include @@ -40,6 +40,7 @@ #include #endif #include +#include "chance.h" #include "empthread.h" #include "file.h" #include "game.h" @@ -129,7 +130,7 @@ update_sched(void *unused) next_update = update_time[0]; if (next_update) { if (update_window > 0) - next_update += random() % update_window; + next_update += roll0(update_window); logerror("Next update at %s", ctime(&next_update)); /* sleep until update is scheduled to go off */ empth_sleep(next_update); diff --git a/src/util/fairland.c b/src/util/fairland.c index 57d79a543..c7f1badda 100644 --- a/src/util/fairland.c +++ b/src/util/fairland.c @@ -29,6 +29,7 @@ * Known contributors to this file: * Ken Stevens, 1995 * Steve McClure, 1998 + * Markus Armbruster, 2004-2012 */ #include @@ -70,6 +71,7 @@ static int quiet = 0; #include #include #include +#include "chance.h" #include "file.h" #include "optlist.h" #include "prototypes.h" @@ -115,7 +117,7 @@ static char *program_name; #define new_x(newx) (((newx) + WORLD_X) % WORLD_X) #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y) -#define rnd(x) (random() % (x)) +#define rnd(x) roll0((x)) static int secs; /* number of sectors grown */ static int ctot; /* total number of continents and islands grown */ @@ -218,7 +220,7 @@ main(int argc, char *argv[]) } parse_args(argc - optind, argv + optind); - srandom(rnd_seed); + seed_prng(rnd_seed); empfile_init(); if (emp_config(config_file) < 0) exit(1);