Author: Andreas Stabel
Date: 23:48:10 04/14/99
Go up one level in this thread
On April 12, 1999 at 19:44:51, James Swafford wrote:
>
>My opening book relies on a random number generation to
>choose among book entries. Windows random( ) function
>leaves a lot to be desired, even when reseeded using the
>system timer before each call.
>
>I'm interested in hearing what other programmers are doing
>for _random_ number generation.
>
>--
>James
Here is my C conversion of the Mersenne Twister which is supposed to be one
of the best pseudo-random generators in existance.
I hope it is useful.
Andreas Stabel
----------- CUT HERE -------------
/* A C-program for MT19937:
// lRand() generates one pseudorandom unsigned integer (32bit)
// which is uniformly distributed among 0 to 2^32-1
// dRand() generates one pseudorandom real number
// which is uniformly distributed among 0.0 to <1.0
// Coded by Takuji Nishimura, considering the suggestions by
// Topher Cooper and Marc Rieffel in July-Aug. 1997.
// Modified by John Cooke 1998-04-20
// to use standard Seed(), lRand(), dRand() functions
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later
// version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Library General Public License for more details.
// You should have received a copy of the GNU Library General
// Public License along with this library; if not, write to the
// Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307 USA
// Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
// When you use this, send an email to: matumoto@math.keio.ac.jp
// with an appropriate reference to your work. */
#include <stdio.h>
#include <time.h>
#define IEEE 1 /* Set = 1 if IEEE 64 bit double float */
#define LITTLE_END 1 /* Set = 1 if running on a little-endian machine */
/* Period parameters */
#define MT_BUFSZ 624
#define N MT_BUFSZ
#define M 397
#define MATRIX_A 0x9908b0df /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */
/* Tempering parameters */
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_L(y) (y >> 18)
typedef unsigned long uint32;
uint32 mt[MT_BUFSZ]; /* the array for the state vector */
int mti;
uint32 mag01[2];
/* mag01[x] = x * MATRIX_A for x=0,1 */
void Seed ( uint32 seed)
{
/* setting initial seeds to mt[N] using
// the generator Line 25 of Table 1 in
// [KNUTH 1981, The Art of Computer Programming
// Vol. 2 (2nd Ed.), pp102] */
/* fudge the seed if its lower bits are zero */
if ((seed & 0xFFFFFF) == 0) seed = seed | 0xFFFFFF;
mt[0] = seed;
/* mt[0] = seed ? seed : 0xffffffff; */
for (mti=1; mti<N; mti++)
mt[mti] = (69069 * mt[mti-1]);
mag01[0] = 0x0;
mag01[1] = MATRIX_A;
}
uint32 lRand()
{
uint32 y;
if (mti >= N) { /* generate N words at one time */
int kk;
for (kk=0;kk<N-M;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<N-1;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);
return y;
}
/* RANGE_LE1 puts a uint32 in the range [0,1] (0<=x<=1) */
#define RANGE_LE1 2.3283064370807974e-10
/* RANGE_LT1 puts a uint32 in the range [0,1) (0<=x<1) */
#define RANGE_LT1 2.3283064365386963e-10
double dRand ()
{
#if IEEE
uint32 hiv,lov, *dvp;
double hdv;
dvp = (uint32 *) &hdv;
hiv = lRand();
lov = lRand();
hiv = hiv & 0xFFFFF;
hiv = hiv | 0x3FF00000;
#if LITTLE_END
dvp[0] = lov; dvp[1] = hiv;
#else
dvp[0] = hiv; dvp[1] = lov;
#endif
return(hdv - (double)1.0);
#else
uint32 y = lRand();
return ( (double)y * RANGE_LT1 ); /* reals: [0,1)-interval */
#endif
}
int main(int argc, char *argp[])
{
unsigned long myseed,hu,lov,hiv, *hup;
double hd;
int two, drflag;
hup = (unsigned long *) &hd; drflag = 0;
if (argc == 1) {
time((long *)&myseed);
} else if (argc >= 2) {
if (sscanf(argp[1],"%lu", &myseed) != 1) drflag = 1;
if (argc > 2) drflag = 1;
} else goto error;
printf("Seed = %lu (%08lX)\n",myseed,myseed);
Seed(myseed);
hu = 4294967295;
hd = (double)hu * RANGE_LT1;
printf("2**32 - 1 * LT1 --> %.20G\n",hd);
hd = (double)hu * RANGE_LE1;
printf("2**32 - 1 * LE1 --> %.20G\n",hd);
printf("hu = %08lX !\n",hu);
hu = 4294967294;
hd = (double)hu * RANGE_LT1;
printf("2**32 - 2 * LT1 --> %.20G\n",hd);
hd = (double)hu * RANGE_LE1;
printf("2**32 - 2 * LE1 --> %.20G\n",hd);
printf("hu = %08lX !\n",hu);
two = 0;
for (;;) {
if (!drflag) {
hu = lRand();
if (two) {
lov = hu; two = 0;
hiv = hiv & 0xFFFFF;
hiv = hiv | 0x3FF00000;
hup[0] = lov; hup[1] = hiv;
hd = hd - (double)1.0;
printf("%12lu (%08lX) ",hu,hu);
printf("%08lX %08lX -> %.18lf",hup[1],hup[0],hd);
} else {
hiv = hu; two = 1;
printf("%12lu (%08lX) ",hu,hu);
}
} else { /* drflag */
hd = dRand();
printf("%08lX %08lX -> %.18lf ",hup[1],hup[0],hd);
}
while (getchar() != '\n') ;
}
return(0);
error:
printf("Error !!!!\n");
return(1);
}
----------- CUT HERE -------------
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.