Switch PRNG from BSD random() to Mersenne Twister

random() may yield different pseudo-random number sequences for the
same seed on another system.  For instance, at least some versions of
MinGW provide a random() in -liberty that differs from traditional BSD
(see commit c8231b12).  Rather inconvenient for regression testing.

MT19937 Mersenne Twister is a proven, high-quality PRNG.  Actual code
is reference code provided by the inventors[*].  Quick tests show
performance comparable to random().

Like random(), MT is not cryptographically secure: observing enough of
its output permits guessing its state, and thus its future output.  I
don't think players can do that.

Drop the copy of BSD random() we added for Windows.

Like the previous commit, this changes the server's die rolls, and
makes fairland create a different random map for the same seed.  Update
expected smoke test results accordingly.

[*] mt19937ar.sep.tgz downloaded from
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
This commit is contained in:
Markus Armbruster 2012-08-25 17:21:10 +02:00
parent b5d8806eb1
commit 39c26f4238
16 changed files with 3799 additions and 4356 deletions

View file

@ -35,6 +35,7 @@
#include <math.h>
#include <stdlib.h>
#include "chance.h"
#include "mt19937ar.h"
/*
* Return non-zero with probability D.
@ -42,7 +43,7 @@
int
chance(double d)
{
return d > (random() % 32768) / 32768.0;
return d > genrand_real2();
}
/*
@ -78,7 +79,7 @@ roll0(int n)
int r;
do
r = random() & (pow2 - 1);
r = genrand_int32() & (pow2 - 1);
while (r >= n);
return r;
}
@ -112,5 +113,5 @@ roundavg(double val)
void
seed_prng(unsigned seed)
{
srandom(seed);
init_genrand(seed);
}