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

@ -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 <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
* 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)

View file

@ -29,6 +29,7 @@
* Known contributors to this file:
* Dave Pare, 1989
* Steve McClure, 1997
* Markus Armbruster, 2004-2012
*/
#include <config.h>
@ -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;
}

View file

@ -27,7 +27,7 @@
* landgun.c: Fire weapons
*
* Known contributors to this file:
* Markus Armbruster, 2006-2009
* Markus Armbruster, 2006-2012
*/
#include <config.h>
@ -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;
}

View file

@ -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 <config.h>
@ -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;