Encapsulate direct use of random(), srandom() in chance.c

Wrap roll0() around random(), and seed_prng() around srandom().  In
preparation of replacing the PRNG.
This commit is contained in:
Markus Armbruster 2012-08-14 19:23:12 +02:00
parent 8eb78a5a80
commit 866859e912
17 changed files with 87 additions and 49 deletions

View file

@ -34,7 +34,9 @@
#define CHANCE_H #define CHANCE_H
extern int chance(double); extern int chance(double);
extern int roll0(int);
extern int roll(int); extern int roll(int);
extern int roundavg(double); extern int roundavg(double);
extern void seed_prng(unsigned);
#endif #endif

View file

@ -29,6 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Ken Stevens, 1995 * Ken Stevens, 1995
* Steve McClure, 1998 * Steve McClure, 1998
* Markus Armbruster, 2006-2012
*/ */
#ifndef DAMAGE_H #ifndef DAMAGE_H
@ -38,11 +39,11 @@
#define DPERCENT_DAMAGE(x) (100.0 * (x) / ((x) + 100.0)) #define DPERCENT_DAMAGE(x) (100.0 * (x) / ((x) + 100.0))
#define DMINE_HITCHANCE(x) ((x) / ((x) + 20.0)) #define DMINE_HITCHANCE(x) ((x) / ((x) + 20.0))
#define DMINE_LHITCHANCE(x) ((x) / ((x) + 35.0)) #define DMINE_LHITCHANCE(x) ((x) / ((x) + 35.0))
#define MINE_DAMAGE() (22 + random()%21) #define MINE_DAMAGE() (22 + roll0(21))
#define MINE_LDAMAGE() (11 + random()%20) #define MINE_LDAMAGE() (11 + roll0(20))
#define DTORP_HITCHANCE(range, vis) \ #define DTORP_HITCHANCE(range, vis) \
(0.9 / ((range)+1) + (((vis) < 6) ? (5-(vis)) * 0.03 : 0.0)) (0.9 / ((range)+1) + (((vis) < 6) ? (5-(vis)) * 0.03 : 0.0))
#define TORP_DAMAGE() (torpedo_damage + (random() % torpedo_damage) + \ #define TORP_DAMAGE() (torpedo_damage + roll0(torpedo_damage) + \
(random() % torpedo_damage)) roll0(torpedo_damage))
#endif #endif

View file

@ -29,6 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Pat Loney, 1992 * Pat Loney, 1992
* Steve McClure, 1997 * Steve McClure, 1997
* Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -123,7 +124,7 @@ anti(void)
sect.sct_mobil = 0; sect.sct_mobil = 0;
} }
sect.sct_loyal = sect.sct_loyal * 0.5; sect.sct_loyal = sect.sct_loyal * 0.5;
n_cheleft = (random() % 4); n_cheleft = roll0(4);
/* 75% chance some che will get left */ /* 75% chance some che will get left */
if (n_cheleft) { if (n_cheleft) {
/* Ok, now leave anywhere from 16% to 25% of the che */ /* Ok, now leave anywhere from 16% to 25% of the che */

View file

@ -331,7 +331,7 @@ launch_sat(struct plnstr *pp)
} }
i = pp->pln_tech + pp->pln_effic; i = pp->pln_tech + pp->pln_effic;
if (chance(1.0 - (i / (i + 50.0)))) { if (chance(1.0 - (i / (i + 50.0)))) {
dir = DIR_FIRST + random() % 6; dir = DIR_FIRST + roll0(6);
sx = xnorm(sx + diroff[dir][0]); sx = xnorm(sx + diroff[dir][0]);
sy = ynorm(sy + diroff[dir][1]); sy = ynorm(sy + diroff[dir][1]);
pr("Your trajectory was a little off.\n"); pr("Your trajectory was a little off.\n");

View file

@ -27,11 +27,12 @@
* news.c: Show current Empire news * news.c: Show current Empire news
* *
* Known contributors to this file: * Known contributors to this file:
* * Markus Armbruster, 2006-2012
*/ */
#include <config.h> #include <config.h>
#include "chance.h"
#include "commands.h" #include "commands.h"
#include "news.h" #include "news.h"
#include "optlist.h" #include "optlist.h"
@ -186,7 +187,7 @@ preport(struct nwsstr *np)
/* /*
* vary the order of the printing of "%d times " * 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); sprintf(cp, "%s times ", ptr);
cp += strlen(cp); cp += strlen(cp);
np->nws_ntm = 1; np->nws_ntm = 1;
@ -194,7 +195,7 @@ preport(struct nwsstr *np)
strcpy(cp, cname(np->nws_ano)); strcpy(cp, cname(np->nws_ano));
cp += strlen(cp); cp += strlen(cp);
*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)); cname(np->nws_vno));
cp += strlen(cp); cp += strlen(cp);
if (np->nws_ntm != 1) { if (np->nws_ntm != 1) {

View file

@ -24,7 +24,7 @@
* *
* --- * ---
* *
* chance.c: return 1 if "roll" is under the chance. * chance.c: Roll dice
* *
* Known contributors to this file: * Known contributors to this file:
* *
@ -36,12 +36,27 @@
#include <stdlib.h> #include <stdlib.h>
#include "chance.h" #include "chance.h"
/*
* Return non-zero with probability D.
*/
int int
chance(double d) chance(double d)
{ {
return d > (random() % 32768) / 32768.0; 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 int
roll(int n) roll(int n)
{ {
@ -49,8 +64,8 @@ roll(int n)
} }
/* /*
* round value to nearest int (on the average). E.g. rounds up * Round VAL to nearest integer (on the average).
* with a chance proportional to the size of the fractional part. * VAL's fractional part is chance to round up.
*/ */
int int
roundavg(double val) roundavg(double val)
@ -58,3 +73,14 @@ roundavg(double val)
double flr = floor(val); double flr = floor(val);
return (int)(flr + chance(val - flr)); 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);
}

View file

@ -29,7 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Ken Stevens, 1995 * Ken Stevens, 1995
* Steve McClure, 1996-2000 * Steve McClure, 1996-2000
* Markus Armbruster, 2006-2011 * Markus Armbruster, 2006-2012
*/ */
#include <config.h> #include <config.h>
@ -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 * since a single dead guy normally wouldn't cause a commander to
* rethink his strategies, but 50 dead guys might. * rethink his strategies, but 50 dead guys might.
*/ */
odds += (random() % 11 - 5) / 100.0; odds += (roll0(11) - 5) / 100.0;
if (odds < 0.0) if (odds < 0.0)
odds = 0.1; odds = 0.1;
if (odds > 1.0) if (odds > 1.0)
odds = 1.0; odds = 1.0;
recalctime = 8 + (random() % 43); recalctime = 8 + roll0(43);
while (!success && ototal) { while (!success && ototal) {
if (chance(odds)) { if (chance(odds)) {
pr("!"); pr("!");
@ -1858,9 +1858,9 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
if (((a_cas + d_cas) % 70) == 69) if (((a_cas + d_cas) % 70) == 69)
pr("\n"); pr("\n");
if (recalctime-- <= 0) { if (recalctime-- <= 0) {
recalctime = 8 + (random() % 43); recalctime = 8 + roll0(43);
odds = att_calcodds(ototal, dtotal); odds = att_calcodds(ototal, dtotal);
odds += (random() % 11 - 5) / 100.0; odds += (roll0(11) - 5) / 100.0;
if (odds < 0.0) if (odds < 0.0)
odds = 0.1; odds = 0.1;
if (odds > 1.0) if (odds > 1.0)

View file

@ -29,6 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1989 * Dave Pare, 1989
* Steve McClure, 1997 * Steve McClure, 1997
* Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -169,7 +170,7 @@ damage(int amt, int pct)
return 0; return 0;
tmp = amt * pct; tmp = amt * pct;
lost = tmp / 100; lost = tmp / 100;
if (random() % 100 < tmp % 100) if (roll0(100) < tmp % 100)
lost++; lost++;
return amt - lost; return amt - lost;
} }

View file

@ -27,7 +27,7 @@
* landgun.c: Fire weapons * landgun.c: Fire weapons
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2006-2009 * Markus Armbruster, 2006-2012
*/ */
#include <config.h> #include <config.h>
@ -48,7 +48,7 @@ fortgun(int effic, int guns)
double d; double d;
double g = MIN(guns, 7); 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; d *= effic / 100.0;
return d; return d;
} }
@ -60,7 +60,7 @@ seagun(int effic, int guns)
d = 0.0; d = 0.0;
while (guns--) while (guns--)
d += 10.0 + random() % 6; d += 10.0 + roll0(6);
d *= effic * 0.01; d *= effic * 0.01;
return d; return d;
} }
@ -72,7 +72,7 @@ landunitgun(int effic, int guns)
d = 0.0; d = 0.0;
while (guns--) while (guns--)
d += 5.0 + random() % 6; d += 5.0 + roll0(6);
d *= effic * 0.01; d *= effic * 0.01;
return d; return d;
} }
@ -221,9 +221,9 @@ lnd_sabo(struct lndstr *lp, short item[])
lp->lnd_item[I_SHELL]--; lp->lnd_item[I_SHELL]--;
dam = fortgun(3 * lp->lnd_effic, 7); dam = fortgun(3 * lp->lnd_effic, 7);
if (item[I_SHELL] > 20) 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) 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; return dam;
} }

View file

@ -29,7 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1986 * Dave Pare, 1986
* Steve McClure, 1996-2000 * Steve McClure, 1996-2000
* Markus Armbruster, 2007-2010 * Markus Armbruster, 2007-2012
*/ */
#include <config.h> #include <config.h>
@ -100,7 +100,7 @@ takeover(struct sctstr *sp, natid newown)
if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic)))) if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
continue; continue;
} }
n = lp->lnd_effic - (30 + (random() % 100)); n = lp->lnd_effic - (30 + roll0(100));
if (n < 0) if (n < 0)
n = 0; n = 0;
lp->lnd_effic = n; lp->lnd_effic = n;
@ -129,7 +129,7 @@ takeover(struct sctstr *sp, natid newown)
* how spunky are these guys? * how spunky are these guys?
* n: random number from -25:75 + (50 - loyalty) * 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) { if (n > 0 && sp->sct_own == sp->sct_oldown) {
che_count = (civ * n / 3000) + 5; che_count = (civ * n / 3000) + 5;
if (che_count * 2 > civ) 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, * XXX If this was done right, planes could escape,
* flying to a nearby friendly airport. * flying to a nearby friendly airport.
*/ */
n = pp->pln_effic - (30 + (random() % 100)); n = pp->pln_effic - (30 + roll0(100));
if (n < 0) if (n < 0)
n = 0; n = 0;
pp->pln_effic = n; pp->pln_effic = n;

View file

@ -29,7 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1986 * Dave Pare, 1986
* Steve McClure, 1996 * Steve McClure, 1996
* Markus Armbruster, 2004-2010 * Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -95,12 +95,12 @@ do_feed(struct sctstr *sp, struct natstr *np, short *vec,
nreport(sp->sct_own, N_DIE_FAMINE, 0, 1); nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
} }
sp->sct_work = 0; sp->sct_work = 0;
sp->sct_loyal += (random() % 8) + 2; sp->sct_loyal += roll0(8) + 2;
} }
sctwork = 0; sctwork = 0;
} else { } else {
if (sp->sct_work < 100) if (sp->sct_work < 100)
sctwork = sp->sct_work + 8 + (random() % 15); sctwork = sp->sct_work + 8 + roll0(15);
if (sctwork > 100) if (sctwork > 100)
sctwork = 100; sctwork = 100;
if (!player->simulation) if (!player->simulation)

View file

@ -28,6 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Steve McClure, 1998-2000 * Steve McClure, 1998-2000
* Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -182,7 +183,7 @@ plague_people(struct natstr *np, short *vec,
} }
if (*ptime <= 0) { if (*ptime <= 0) {
*pstage -= 1; *pstage -= 1;
*ptime = etus / 2 + random() % etus; *ptime = etus / 2 + roll0(etus);
} }
return stage; return stage;
} }

View file

@ -28,6 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1986 * Dave Pare, 1986
* Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -72,7 +73,7 @@ populace(struct natstr *np, struct sctstr *sp, int etu)
n = roundavg(etu * 0.125); n = roundavg(etu * 0.125);
if (n == 0) if (n == 0)
n = 1; n = 1;
n = sp->sct_loyal + (random() % n) + 1; n = sp->sct_loyal + roll0(n) + 1;
if (n > 127) if (n > 127)
n = 127; n = 127;
sp->sct_loyal = n; 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) { if (sp->sct_loyal > 65 && mil < civ / 20) {
int work_red; 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; n = sp->sct_work - work_red;
if (n < 0) if (n < 0)
n = 0; n = 0;

View file

@ -29,7 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1986 * Dave Pare, 1986
* Steve McClure, 1997-2000 * Steve McClure, 1997-2000
* Markus Armbruster, 2004-2011 * Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -66,7 +66,7 @@ revolt(struct sctstr *sp)
return; return;
che_uw = 0; che_uw = 0;
/* che due to civilian unrest */ /* che due to civilian unrest */
n = 10 - (random() % 20); n = 10 - roll0(20);
che_civ = 3 + (civ * n / 500); che_civ = 3 + (civ * n / 500);
if (che_civ < 0) if (che_civ < 0)
che_civ = 0; che_civ = 0;
@ -77,7 +77,7 @@ revolt(struct sctstr *sp)
che += che_civ; che += che_civ;
if (che < CHE_MAX) { if (che < CHE_MAX) {
/* che due to uw unrest */ /* che due to uw unrest */
n = 10 + (random() % 30); n = 10 + roll0(30);
che_uw = 5 + (uw * n / 500); che_uw = 5 + (uw * n / 500);
if (che_uw > uw) if (che_uw > uw)
che_uw = uw; che_uw = uw;
@ -240,7 +240,7 @@ guerrilla(struct sctstr *sp)
} }
if (mil > 0) { if (mil > 0) {
/* military won. */ /* military won. */
n = sp->sct_loyal - (random() % 15); n = sp->sct_loyal - roll0(15);
if (n < 0) if (n < 0)
n = 0; n = 0;
sp->sct_loyal = n; sp->sct_loyal = n;
@ -254,7 +254,7 @@ guerrilla(struct sctstr *sp)
* guerrillas have to resort to blowing things up. * guerrillas have to resort to blowing things up.
* Note this disrupts work in the sector. * Note this disrupts work in the sector.
*/ */
n = (random() % 10) + (random() % che); n = roll0(10) + roll0(che);
if (n > 100) if (n > 100)
n = 100; n = 100;
tmp = sp->sct_work - n; tmp = sp->sct_work - n;
@ -327,14 +327,14 @@ guerrilla(struct sctstr *sp)
/* loyalty drops during recruitment efforts */ /* loyalty drops during recruitment efforts */
n = sp->sct_loyal; n = sp->sct_loyal;
if (n < 30) if (n < 30)
n += (random() % 5) + 1; n += roll0(5) + 1;
else if (n < 70) else if (n < 70)
n += (random() % 10) + 4; n += roll0(10) + 4;
if (n > 127) if (n > 127)
n = 127; n = 127;
sp->sct_loyal = n; sp->sct_loyal = n;
if (sp->sct_oldown != sp->sct_own || n > 100) { 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)); n /= hap_fact(tnat, getnatp(sp->sct_oldown));
if (n + che > CHE_MAX) if (n + che > CHE_MAX)
n = CHE_MAX - che; n = CHE_MAX - che;
@ -342,7 +342,7 @@ guerrilla(struct sctstr *sp)
civ -= n; civ -= n;
sp->sct_item[I_CIVIL] = civ; sp->sct_item[I_CIVIL] = civ;
} }
n = uw * (random() % 3) / 200; n = uw * roll0(3) / 200;
if (n + che > CHE_MAX) if (n + che > CHE_MAX)
n = CHE_MAX - che; n = CHE_MAX - che;
che += n; che += n;
@ -375,7 +375,7 @@ domove:
val = roundintby(val, 10); val = roundintby(val, 10);
/* inject a modicum of indeterminism; also /* inject a modicum of indeterminism; also
* avoids che preferring certain directions */ * avoids che preferring certain directions */
val += random() % 10 - 5; val += roll0(10) - 5;
if (val >= min_mil) if (val >= min_mil)
continue; continue;
nicest_sp = nsp; nicest_sp = nsp;

View file

@ -50,6 +50,7 @@
#include "sys/socket.h" #include "sys/socket.h"
#endif #endif
#include "chance.h"
#include "empio.h" #include "empio.h"
#include "empthread.h" #include "empthread.h"
#include "file.h" #include "file.h"
@ -349,7 +350,7 @@ crash_dump(void)
static void static void
init_server(unsigned seed, int force_bad_state) init_server(unsigned seed, int force_bad_state)
{ {
srandom(seed); seed_prng(seed);
#if defined(_WIN32) #if defined(_WIN32)
loc_NTInit(); loc_NTInit();
#endif #endif

View file

@ -30,7 +30,7 @@
* Dave Pare, 1994 * Dave Pare, 1994
* Steve McClure, 1996 * Steve McClure, 1996
* Ron Koenderink, 2005 * Ron Koenderink, 2005
* Markus Armbruster, 2007-2010 * Markus Armbruster, 2007-2012
*/ */
#include <config.h> #include <config.h>
@ -40,6 +40,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
#include <time.h> #include <time.h>
#include "chance.h"
#include "empthread.h" #include "empthread.h"
#include "file.h" #include "file.h"
#include "game.h" #include "game.h"
@ -129,7 +130,7 @@ update_sched(void *unused)
next_update = update_time[0]; next_update = update_time[0];
if (next_update) { if (next_update) {
if (update_window > 0) if (update_window > 0)
next_update += random() % update_window; next_update += roll0(update_window);
logerror("Next update at %s", ctime(&next_update)); logerror("Next update at %s", ctime(&next_update));
/* sleep until update is scheduled to go off */ /* sleep until update is scheduled to go off */
empth_sleep(next_update); empth_sleep(next_update);

View file

@ -29,6 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Ken Stevens, 1995 * Ken Stevens, 1995
* Steve McClure, 1998 * Steve McClure, 1998
* Markus Armbruster, 2004-2012
*/ */
#include <config.h> #include <config.h>
@ -70,6 +71,7 @@ static int quiet = 0;
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "chance.h"
#include "file.h" #include "file.h"
#include "optlist.h" #include "optlist.h"
#include "prototypes.h" #include "prototypes.h"
@ -115,7 +117,7 @@ static char *program_name;
#define new_x(newx) (((newx) + WORLD_X) % WORLD_X) #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
#define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y) #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 secs; /* number of sectors grown */
static int ctot; /* total number of continents and islands 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); parse_args(argc - optind, argv + optind);
srandom(rnd_seed); seed_prng(rnd_seed);
empfile_init(); empfile_init();
if (emp_config(config_file) < 0) if (emp_config(config_file) < 0)
exit(1); exit(1);