FORTRAN Computer Games


Mersenne Twister

The Mersenne Twister is a general purpose pseudo-random number generator, developed in 1997 by Makoto Matsumoto and Takuji Nishimura.

Compile the FORTRAN source in mt19937.f and link the object file with example program TEST. The FORTRAN 77 program initialises the PRNG with the current time delta, then prints 120 random numbers:

C     ******************************************************************
C
C     TEST PROGRAM FOR MERSENNE TWISTER (MT19937).
C
C     ******************************************************************
      PROGRAM TEST
      EXTERNAL SGRND
      REAL*8   GRND
      INTEGER  I, J, ISEED
C
C     SET AND OUTPUT PRNG SEED VALUE.
C
      ISEED = TIME()
      CALL SGRND(ISEED)
      PRINT 100, ISEED
  100 FORMAT (' MERSENNE TWISTER',/,' SEED: ',I10,/)
C
C     PRINT 120 RANDOM NUMBERS.
C
      DO 10 I = 1, 20
      PRINT 200, (GRND(), J = 1, 6)
   10 CONTINUE
  200 FORMAT (6(X,F8.6))
      END

The function TIME() has to be provided by the compiler. To build the source with GNU Fortran, run:

$ gfortran -fno-range-check -c mt19937.f
$ gfortran -o test test.f mt19937.o

The test program then outputs the seed, followed by a batch of random numbers:

$ ./test
 MERSENNE TWISTER
 SEED: 1619448774

 0.002282 0.261516 0.247671 0.291327 0.843831 0.441386
 0.767320 0.131605 0.243605 0.914816 0.199729 0.384769
 0.521070 0.161836 0.646428 0.260970 0.153433 0.306368
 0.475345 0.107536 0.460790 0.839820 0.112740 0.793171
 0.964610 0.931927 0.124288 0.863997 0.671106 0.416717
 0.661144 0.559762 0.391374 0.183029 0.564272 0.523289
 0.203854 0.418752 0.482456 0.679678 0.037317 0.394991
 0.953748 0.173723 0.803380 0.488424 0.828065 0.366737
 0.892184 0.454422 0.774777 0.754022 0.086881 0.873094
 0.911008 0.394991 0.540264 0.517282 0.734855 0.511451
 0.873087 0.746678 0.485112 0.758031 0.091812 0.973301
 0.627849 0.666051 0.585444 0.906104 0.388585 0.234770
 0.228406 0.952953 0.994076 0.503377 0.883810 0.844211
 0.585209 0.768527 0.229184 0.265604 0.128610 0.786664
 0.938202 0.844929 0.384856 0.335330 0.606068 0.560731
 0.110499 0.770378 0.910298 0.083177 0.210712 0.301827
 0.117783 0.678383 0.230553 0.222373 0.756828 0.572687
 0.860481 0.839519 0.269958 0.322089 0.030174 0.686624
 0.432446 0.573208 0.315556 0.423841 0.601680 0.830107
 0.662937 0.903037 0.140810 0.564939 0.596350 0.426825

References


Home