--- lachesis-0.2.5/engine/random.cc	2004-01-15 23:28:15.000000000 -0800
+++ lachesis/engine/random.cc	2010-02-08 12:01:51.000000000 -0800
@@ -1,6 +1,6 @@
 /*
  * Lachesis, an IRCRPG combat engine - Random number generation
- * Copyright 2003-2004 M. Dennis
+ * Copyright 2003-2010 M. Dennis
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -16,6 +16,55 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
+ * This file includes third-party code for random number generation.
+ * The following copyright notice applies to said code, and all such
+ * code will be contained in function names beginning with MT_
+ */
+
+/* 
+   A C-program for MT19937, with initialization improved 2002/1/26.
+   Coded by Takuji Nishimura and Makoto Matsumoto.
+
+   Before using, initialize the state by using init_genrand(seed)  
+   or init_by_array(init_key, key_length).
+
+   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+   All rights reserved.                          
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+     1. Redistributions of source code must retain the above copyright
+        notice, this list of conditions and the following disclaimer.
+
+     2. Redistributions in binary form must reproduce the above copyright
+        notice, this list of conditions and the following disclaimer in the
+        documentation and/or other materials provided with the distribution.
+
+     3. The names of its contributors may not be used to endorse or promote 
+        products derived from this software without specific prior written 
+        permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+   Any feedback is very welcome.
+   http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
+   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
+*/
+
+/*
  * Since the random elements of the formulas are stated as dice rolls,
  * the random number generation will operate on that basis.
  */
@@ -29,30 +78,104 @@
 #include "../config.h"
 #include "../types.h"
 
-static char* id="@(#) $Id: random.cc,v 1.1.1.1 2004/01/16 07:28:15 lachesis Exp $";
+/* Period parameters */
+#define MT_N 624
+#define MT_M 397
+#define MT_MATRIX_A 0x9908b0dfUL   /* constant vector a */
+#define MT_UPPER_MASK 0x80000000UL /* most significant w-r bits */
+#define MT_LOWER_MASK 0x7fffffffUL /* least significant r bits */
+
+static unsigned long MT_mt[MT_N]; /* the array for the state vector  */
+static int MT_mti=MT_N+1; /* mti==N+1 means mt[N] is not initialized */
+
+/* initializes mt[N] with a seed */
+void MT_init_genrand(unsigned long s)
+{
+  MT_mt[0]= s & 0xffffffffUL;
+  for (MT_mti=1; MT_mti<MT_N; MT_mti++) {
+    MT_mt[MT_mti] = (1812433253UL * (MT_mt[MT_mti-1] ^ (MT_mt[MT_mti-1] >> 30)) + MT_mti);
+    /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+    /* In the previous versions, MSBs of the seed affect   */
+    /* only MSBs of the array mt[].                        */
+    /* 2002/01/09 modified by Makoto Matsumoto             */
+    MT_mt[MT_mti] &= 0xffffffffUL;
+    /* for >32 bit machines */
+  }
+}
+
+/* generates a random number on [0,0xffffffff]-interval */
+unsigned long MT_genrand_int32(void)
+{
+  unsigned long y;
+  static unsigned long mag01[2]={0x0UL, MT_MATRIX_A};
+  /* mag01[x] = x * MATRIX_A  for x=0,1 */
+
+  if (MT_mti >= MT_N) { /* generate N words at one time */
+    int kk;
+
+    if (MT_mti == MT_N+1)   /* if init_genrand() has not been called, */
+      MT_init_genrand(5489UL); /* a default initial seed is used */
+
+    for (kk=0;kk<MT_N-MT_M;kk++) {
+      y = (MT_mt[kk]&MT_UPPER_MASK)|(MT_mt[kk+1]&MT_LOWER_MASK);
+      MT_mt[kk] = MT_mt[kk+MT_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
+    }
+    for (;kk<MT_N-1;kk++) {
+      y = (MT_mt[kk]&MT_UPPER_MASK)|(MT_mt[kk+1]&MT_LOWER_MASK);
+      MT_mt[kk] = MT_mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
+    }
+    y = (MT_mt[MT_N-1]&MT_UPPER_MASK)|(MT_mt[0]&MT_LOWER_MASK);
+    MT_mt[MT_N-1] = MT_mt[MT_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
+
+    MT_mti = 0;
+    }
+  
+    y = MT_mt[MT_mti++];
+
+    /* Tempering */
+    y ^= (y >> 11);
+    y ^= (y << 7) & 0x9d2c5680UL;
+    y ^= (y << 15) & 0xefc60000UL;
+    y ^= (y >> 18);
+
+    return y;
+}
 
 TF Random_Roll(unsigned long count, unsigned long magnitude, long floor,
 	       long **rolls)
 {
-  unsigned long excess, loop;
+  //unsigned long mask, loop;
+  unsigned long loop;
 
   if (rolls==NULL)
     return FALSE;
   if (count==0||count>DICE_MAX)
     return FALSE;
-  if (magnitude<2)
+  if (magnitude<2 || magnitude>0x80000000UL)
     return FALSE;
+
+  if (MT_mti==MT_N+1) {
+    MT_init_genrand(time(NULL));  //Gives just enough non-determinism,
+    MT_genrand_int32();  //however this is very bad from an
+    MT_genrand_int32();  //unpredictability standpoint, since careful
+    //timing of the first roll request can control all subsequent
+    //results...
+  }
+ 
   *rolls = new long[count+1];
   **rolls = 0;
   for (loop=0;loop<count;loop++) {
-    excess = RAND_MAX / magnitude;
-    if (excess==0) {
-      delete [] *rolls;
-      return FALSE;
-    }
-    do
-      (*rolls)[loop+1] = rand() / excess;
-    while ((*rolls)[loop+1]>=magnitude);
+    do {
+      int mtemp = magnitude, count;
+      (*rolls)[loop+1] = MT_genrand_int32() & 0x7fffffffUL;
+      //mask = 0xfffffffeUL;
+      //while ((magnitude - 1) & mask)
+      //  mask <<= 1;
+      //(*rolls)[loop+1] &= ~mask;
+      for (count=0;mtemp>0;count++)
+	mtemp >>= 1;
+      (*rolls)[loop+1] >>= 31 - count;
+    } while ((*rolls)[loop+1] > magnitude - 1);
     if (floor<0) {
       unsigned long sub = (unsigned long)(-1*floor);
       if (sub>(*rolls)[loop+1])
@@ -76,7 +199,7 @@
   // is the more recent rule, but now both are supported based on
   // WROLL_USES_SUCC_FLAG
 
-  unsigned long excess, loop;
+  unsigned long loop;
 #ifdef WROLL_USES_SUCC_FLAG
   TF success = FALSE;
 #endif
@@ -87,12 +210,18 @@
     return FALSE;
   if (threshold<2||threshold>10)
     return FALSE;
+
+  if (MT_mti==MT_N+1) {
+    MT_init_genrand(time(NULL));
+    MT_genrand_int32();
+    MT_genrand_int32();
+  }
+
   *rolls = new long[count+1];
   **rolls = 0;
   for (loop=0;loop<count;loop++) {
-    excess = RAND_MAX / 10;
     do
-      (*rolls)[loop+1] = rand() / excess + 1;
+      (*rolls)[loop+1] = (MT_genrand_int32() & 0xfUL) + 1;
     while ((*rolls)[loop+1]>10);
 
     if ((*rolls)[loop+1]==1)
