]> git.pond.sub.org Git - empserver/commitdiff
Encapsulate direct use of random(), srandom() in chance.c
authorMarkus Armbruster <armbru@pond.sub.org>
Tue, 14 Aug 2012 17:23:12 +0000 (19:23 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 8 May 2013 04:55:20 +0000 (06:55 +0200)
Wrap roll0() around random(), and seed_prng() around srandom().  In
preparation of replacing the PRNG.

17 files changed:
include/chance.h
include/damage.h
src/lib/commands/anti.c
src/lib/commands/laun.c
src/lib/commands/news.c
src/lib/gen/chance.c
src/lib/subs/attsub.c
src/lib/subs/damage.c
src/lib/subs/landgun.c
src/lib/subs/takeover.c
src/lib/update/human.c
src/lib/update/plague.c
src/lib/update/populace.c
src/lib/update/revolt.c
src/server/main.c
src/server/update.c
src/util/fairland.c

index 194dcf02a53a9004865eabb2b1a9851a325b2274..da5d18852b8047bbc0ecf2e9b09a3e1e41576f6b 100644 (file)
@@ -34,7 +34,9 @@
 #define CHANCE_H
 
 extern int chance(double);
 #define CHANCE_H
 
 extern int chance(double);
+extern int roll0(int);
 extern int roll(int);
 extern int roundavg(double);
 extern int roll(int);
 extern int roundavg(double);
+extern void seed_prng(unsigned);
 
 #endif
 
 #endif
index 3ef3a334cafdfbbe80f40a4d8c89b96e741f359b..09861f1a458fa271646276bd2cbad194b7416ce0 100644 (file)
@@ -29,6 +29,7 @@
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2006-2012
  */
 
 #ifndef DAMAGE_H
  */
 
 #ifndef DAMAGE_H
 #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 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 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
 
 #endif
index 174b1dd2ba449814f5480362132ef056020c735e..7c5ac1bed138c30faecb1350b95f3ef06571778f 100644 (file)
@@ -29,6 +29,7 @@
  *  Known contributors to this file:
  *     Pat Loney, 1992
  *     Steve McClure, 1997
  *  Known contributors to this file:
  *     Pat Loney, 1992
  *     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_loyal = sect.sct_loyal * 0.5;
                    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 */
                /* 75% chance some che will get left */
                if (n_cheleft) {
                    /* Ok, now leave anywhere from 16% to 25% of the che */
index 3078782a1f5663f59797d6ae0f3651e6d53c9482..4f226be99056923145ab9a40e999b4c033fe7444 100644 (file)
@@ -331,7 +331,7 @@ launch_sat(struct plnstr *pp)
     }
     i = pp->pln_tech + pp->pln_effic;
     if (chance(1.0 - (i / (i + 50.0)))) {
     }
     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");
        sx = xnorm(sx + diroff[dir][0]);
        sy = ynorm(sy + diroff[dir][1]);
        pr("Your trajectory was a little off.\n");
index 8fa0b2a6e20cdc073c423897dbae3818b7925e9e..2ec5ef6457d0e2ae7e513edc8c494514280d6c95 100644 (file)
  *  news.c: Show current Empire news
  *
  *  Known contributors to this file:
  *  news.c: Show current Empire news
  *
  *  Known contributors to this file:
- *
+ *     Markus Armbruster, 2006-2012
  */
 
 #include <config.h>
 
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "commands.h"
 #include "news.h"
 #include "optlist.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 "
      */
     /*
      * 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;
        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++ = ' ';
     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) {
            cname(np->nws_vno));
     cp += strlen(cp);
     if (np->nws_ntm != 1) {
index 284680b1bb946ff1f8ae9f4e09ab1bd30945b234..1738ab280b03efc9b033b763de909cdd9882390c 100644 (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:
  *
 #include <stdlib.h>
 #include "chance.h"
 
 #include <stdlib.h>
 #include "chance.h"
 
+/*
+ * Return non-zero with probability D.
+ */
 int
 chance(double d)
 {
     return d > (random() % 32768) / 32768.0;
 }
 
 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)
 {
 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)
  */
 int
 roundavg(double val)
@@ -58,3 +73,14 @@ roundavg(double val)
     double flr = floor(val);
     return (int)(flr + chance(val - flr));
 }
     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);
+}
index 292c71f754ca22ba75493947f1f293626bb80274..2d5a169cb736b3e1b886a44b456fc485543e31b8 100644 (file)
@@ -29,7 +29,7 @@
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1996-2000
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     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
      * rethink his strategies, but 50 dead guys might.
      */
      * 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;
     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("!");
     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) {
        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 = 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)
            if (odds < 0.0)
                odds = 0.1;
            if (odds > 1.0)
index 4c0f8f9c1a530e488b0904d7e295c436f7ba6059..0005d7dbf2990230eb61ac8522becb78839c3722 100644 (file)
@@ -29,6 +29,7 @@
  *  Known contributors to this file:
  *     Dave Pare, 1989
  *     Steve McClure, 1997
  *  Known contributors to this file:
  *     Dave Pare, 1989
  *     Steve McClure, 1997
+ *     Markus Armbruster, 2004-2012
  */
 
 #include <config.h>
  */
 
 #include <config.h>
@@ -169,7 +170,7 @@ damage(int amt, int pct)
        return 0;
     tmp = amt * pct;
     lost = tmp / 100;
        return 0;
     tmp = amt * pct;
     lost = tmp / 100;
-    if (random() % 100 < tmp % 100)
+    if (roll0(100) < tmp % 100)
        lost++;
     return amt - lost;
 }
        lost++;
     return amt - lost;
 }
index fc6f3607935ae71fced7690e4acd9fddea404369..e7bafa4ee82c79dd0e435e822f05eb8fd9cb4a0a 100644 (file)
@@ -27,7 +27,7 @@
  *  landgun.c: Fire weapons
  *
  *  Known contributors to this file:
  *  landgun.c: Fire weapons
  *
  *  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 g = MIN(guns, 7);
 
     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;
 }
     d *= effic / 100.0;
     return d;
 }
@@ -60,7 +60,7 @@ seagun(int effic, int guns)
 
     d = 0.0;
     while (guns--)
 
     d = 0.0;
     while (guns--)
-       d += 10.0 + random() % 6;
+       d += 10.0 + roll0(6);
     d *= effic * 0.01;
     return d;
 }
     d *= effic * 0.01;
     return d;
 }
@@ -72,7 +72,7 @@ landunitgun(int effic, int guns)
 
     d = 0.0;
     while (guns--)
 
     d = 0.0;
     while (guns--)
-       d += 5.0 + random() % 6;
+       d += 5.0 + roll0(6);
     d *= effic * 0.01;
     return d;
 }
     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)
     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)
     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;
 }
 
index 3a2ec8ead682e67a81cdeb653ca822cee280cf18..12dae13bc78615d82fb73e8ac9544cd30ea43af9 100644 (file)
@@ -29,7 +29,7 @@
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996-2000
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     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))))
                continue;
        }
            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;
        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)
      */
      * 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)
     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.
      */
      * 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;
     if (n < 0)
        n = 0;
     pp->pln_effic = n;
index 56d0a8fd6910f524e253eeb8a01bc0e21c5a4707..10b8d4600befc700365e56abe23fff327b3d296b 100644 (file)
@@ -29,7 +29,7 @@
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     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);
                }
                sp->sct_work = 0;
                        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 = 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)
            if (sctwork > 100)
                sctwork = 100;
            if (!player->simulation)
index 0cfbfe3716ccc0dc009ad2f5735e87fd0767b8b7..df179e0b9d149e8f873567022bf56c6cb48f0d43 100644 (file)
@@ -28,6 +28,7 @@
  *
  *  Known contributors to this file:
  *     Steve McClure, 1998-2000
  *
  *  Known contributors to this file:
  *     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) {
        *pstage -= 1;
     }
     if (*ptime <= 0) {
        *pstage -= 1;
-       *ptime = etus / 2 + random() % etus;
+       *ptime = etus / 2 + roll0(etus);
     }
     return stage;
 }
     }
     return stage;
 }
index e87da3ce8bc7521e6b45cd4841456b0326d9deb4..4347a448d67dfb3a24813acc6fe841223cd6518b 100644 (file)
@@ -28,6 +28,7 @@
  *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *
  *  Known contributors to this file:
  *     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);
        if (n == 0)
            n = 1;
        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;
        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;
 
     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;
        n = sp->sct_work - work_red;
        if (n < 0)
            n = 0;
index 4818f1d94b4a9d76763e2998dc16b950c78d8f32..dbf2766e690df84cb6ca2339a3efcddb788b7e12 100644 (file)
@@ -29,7 +29,7 @@
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1997-2000
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     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;
     che_uw = 0;
     /* che due to civilian unrest */
        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;
     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 */
     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;
        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.  */
        }
        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;
            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.
         */
         * 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;
        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)
        /* loyalty drops during recruitment efforts */
        n = sp->sct_loyal;
        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)
            n = 127;
        sp->sct_loyal = n;
        if (sp->sct_oldown != sp->sct_own || n > 100) {
        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;
            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;
        }
            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;
        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 = 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;
            if (val >= min_mil)
                continue;
            nicest_sp = nsp;
index 6a28d5c9fa7ad1b7314c03dae28147f18d6b6d93..9cb2761ddac185fb69b5c33460a9e3c05a799402 100644 (file)
@@ -50,6 +50,7 @@
 #include "sys/socket.h"
 #endif
 
 #include "sys/socket.h"
 #endif
 
+#include "chance.h"
 #include "empio.h"
 #include "empthread.h"
 #include "file.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)
 {
 static void
 init_server(unsigned seed, int force_bad_state)
 {
-    srandom(seed);
+    seed_prng(seed);
 #if defined(_WIN32)
     loc_NTInit();
 #endif
 #if defined(_WIN32)
     loc_NTInit();
 #endif
index 21bda9cb9d3360970052381ce83603082895cf36..51a571b99caf2a222f306cf60a92f3ef472bfd28 100644 (file)
@@ -30,7 +30,7 @@
  *     Dave Pare, 1994
  *     Steve McClure, 1996
  *     Ron Koenderink, 2005
  *     Dave Pare, 1994
  *     Steve McClure, 1996
  *     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>
 #endif
 #include <time.h>
 #include <sys/wait.h>
 #endif
 #include <time.h>
+#include "chance.h"
 #include "empthread.h"
 #include "file.h"
 #include "game.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 = 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);
            logerror("Next update at %s", ctime(&next_update));
            /* sleep until update is scheduled to go off */
            empth_sleep(next_update);
index 57d79a543efbd344a541864e2215983263e3e340..c7f1baddaf7091f4257a189a7f69934153b887e7 100644 (file)
@@ -29,6 +29,7 @@
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     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 <stdio.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <unistd.h>
+#include "chance.h"
 #include "file.h"
 #include "optlist.h"
 #include "prototypes.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 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 */
 
 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);
 
     }
     parse_args(argc - optind, argv + optind);
 
-    srandom(rnd_seed);
+    seed_prng(rnd_seed);
     empfile_init();
     if (emp_config(config_file) < 0)
        exit(1);
     empfile_init();
     if (emp_config(config_file) < 0)
        exit(1);