]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/chance.c
Switch PRNG from BSD random() to Mersenne Twister
[empserver] / src / lib / gen / chance.c
index 61a5982930b10c1de00db3a9c75d5e984ab1a36f..2a00789fd0b2e4a1094271fc5c52e7970736e1b5 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *
  *  ---
  *
- *  chance.c: return 1 if "roll" is under the chance.
- * 
+ *  chance.c: Roll dice
+ *
  *  Known contributors to this file:
- *     
+ *     Markus Armbruster, 2006-2012
  */
 
 #include <config.h>
 
-#include "gen.h"
+#include <math.h>
+#include <stdlib.h>
+#include "chance.h"
+#include "mt19937ar.h"
 
+/*
+ * Return non-zero with probability D.
+ */
 int
 chance(double d)
 {
-    double roll;
+    return d > genrand_real2();
+}
 
-    roll = (random() & 0x7fff);
+/*
+ * Return non-zero with probability PCT%.
+ */
+int
+pct_chance(int pct)
+{
+    return roll(100) <= pct;
+}
 
-    if (d > roll / 32768.0)
-       return 1;
-    return 0;
+static unsigned
+round_up_to_pow2(unsigned val)
+{
+    val--;
+    val |= val >> 1;
+    val |= val >> 2;
+    val |= val >> 4;
+    val |= val >> 8;
+    val |= val >> 16;
+    val++;
+    return val;
 }
 
+/*
+ * Return a random number in [0..N-1].
+ * N must be in [1..2^31-1].
+ */
+int
+roll0(int n)
+{
+    unsigned pow2 = round_up_to_pow2(n);
+    int r;
+
+    do
+       r = genrand_int32() & (pow2 - 1);
+    while (r >= n);
+    return r;
+}
+
+/*
+ * Return a random number in [1..N].
+ * N must be in [0..2^31-1].
+ */
 int
 roll(int n)
 {
-    return (random() % n) + 1;
+    return 1 + roll0(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 flr;
+    double flr = floor(val);
+    return (int)(flr + chance(val - flr));
+}
 
-    flr = (int)val;
-    if (val < 0)
-       flr -= chance(flr - val);
-    else
-       flr += chance(val - flr);
-    return 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)
+{
+    init_genrand(seed);
 }