Blame rng/mt.c

Packit 67cb25
/* This program is free software; you can redistribute it and/or
Packit 67cb25
   modify it under the terms of the GNU General Public License as
Packit 67cb25
   published by the Free Software Foundation; either version 3 of the
Packit 67cb25
   License, or (at your option) any later version.
Packit 67cb25
Packit 67cb25
   This program is distributed in the hope that it will be useful,
Packit 67cb25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
   General Public License for more details.  You should have received
Packit 67cb25
   a copy of the GNU General Public License along with this library; if
Packit 67cb25
   not, write to the Free Software Foundation, Inc., 51 Franklin Street,
Packit 67cb25
   Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
Packit 67cb25
   Original implementation was copyright (C) 1997 Makoto Matsumoto and
Packit 67cb25
   Takuji Nishimura. Coded by Takuji Nishimura, considering the
Packit 67cb25
   suggestions by Topher Cooper and Marc Rieffel in July-Aug. 1997, "A
Packit 67cb25
   C-program for MT19937: Integer version (1998/4/6)"
Packit 67cb25
Packit 67cb25
   This implementation copyright (C) 1998 Brian Gough. I reorganized
Packit 67cb25
   the code to use the module framework of GSL.  The license on this
Packit 67cb25
   implementation was changed from LGPL to GPL, following paragraph 3
Packit 67cb25
   of the LGPL, version 2.
Packit 67cb25
Packit 67cb25
   Update:
Packit 67cb25
Packit 67cb25
   The seeding procedure has been updated to match the 10/99 release
Packit 67cb25
   of MT19937.
Packit 67cb25
Packit 67cb25
   Update:
Packit 67cb25
Packit 67cb25
   The seeding procedure has been updated again to match the 2002
Packit 67cb25
   release of MT19937
Packit 67cb25
Packit 67cb25
   The original code included the comment: "When you use this, send an
Packit 67cb25
   email to: matumoto@math.keio.ac.jp with an appropriate reference to
Packit 67cb25
   your work".
Packit 67cb25
Packit 67cb25
   Makoto Matsumoto has a web page with more information about the
Packit 67cb25
   generator, http://www.math.keio.ac.jp/~matumoto/emt.html. 
Packit 67cb25
Packit 67cb25
   The paper below has details of the algorithm.
Packit 67cb25
Packit 67cb25
   From: Makoto Matsumoto and Takuji Nishimura, "Mersenne Twister: A
Packit 67cb25
   623-dimensionally equidistributerd uniform pseudorandom number
Packit 67cb25
   generator". ACM Transactions on Modeling and Computer Simulation,
Packit 67cb25
   Vol. 8, No. 1 (Jan. 1998), Pages 3-30
Packit 67cb25
Packit 67cb25
   You can obtain the paper directly from Makoto Matsumoto's web page.
Packit 67cb25
Packit 67cb25
   The period of this generator is 2^{19937} - 1.
Packit 67cb25
Packit 67cb25
*/
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
Packit 67cb25
static inline unsigned long int mt_get (void *vstate);
Packit 67cb25
static double mt_get_double (void *vstate);
Packit 67cb25
static void mt_set (void *state, unsigned long int s);
Packit 67cb25
Packit 67cb25
#define N 624   /* Period parameters */
Packit 67cb25
#define M 397
Packit 67cb25
Packit 67cb25
/* most significant w-r bits */
Packit 67cb25
static const unsigned long UPPER_MASK = 0x80000000UL;   
Packit 67cb25
Packit 67cb25
/* least significant r bits */
Packit 67cb25
static const unsigned long LOWER_MASK = 0x7fffffffUL;   
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    unsigned long mt[N];
Packit 67cb25
    int mti;
Packit 67cb25
  }
Packit 67cb25
mt_state_t;
Packit 67cb25
Packit 67cb25
static inline unsigned long
Packit 67cb25
mt_get (void *vstate)
Packit 67cb25
{
Packit 67cb25
  mt_state_t *state = (mt_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  unsigned long k ;
Packit 67cb25
  unsigned long int *const mt = state->mt;
Packit 67cb25
Packit 67cb25
#define MAGIC(y) (((y)&0x1) ? 0x9908b0dfUL : 0)
Packit 67cb25
Packit 67cb25
  if (state->mti >= N)
Packit 67cb25
    {   /* generate N words at one time */
Packit 67cb25
      int kk;
Packit 67cb25
Packit 67cb25
      for (kk = 0; kk < N - M; kk++)
Packit 67cb25
        {
Packit 67cb25
          unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
Packit 67cb25
          mt[kk] = mt[kk + M] ^ (y >> 1) ^ MAGIC(y);
Packit 67cb25
        }
Packit 67cb25
      for (; kk < N - 1; kk++)
Packit 67cb25
        {
Packit 67cb25
          unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
Packit 67cb25
          mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ MAGIC(y);
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      {
Packit 67cb25
        unsigned long y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
Packit 67cb25
        mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ MAGIC(y);
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
      state->mti = 0;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* Tempering */
Packit 67cb25
  
Packit 67cb25
  k = mt[state->mti];
Packit 67cb25
  k ^= (k >> 11);
Packit 67cb25
  k ^= (k << 7) & 0x9d2c5680UL;
Packit 67cb25
  k ^= (k << 15) & 0xefc60000UL;
Packit 67cb25
  k ^= (k >> 18);
Packit 67cb25
Packit 67cb25
  state->mti++;
Packit 67cb25
Packit 67cb25
  return k;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static double
Packit 67cb25
mt_get_double (void * vstate)
Packit 67cb25
{
Packit 67cb25
  return mt_get (vstate) / 4294967296.0 ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
mt_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  mt_state_t *state = (mt_state_t *) vstate;
Packit 67cb25
  int i;
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    s = 4357;   /* the default seed is 4357 */
Packit 67cb25
Packit 67cb25
  state->mt[0]= s & 0xffffffffUL;
Packit 67cb25
Packit 67cb25
  for (i = 1; i < N; i++)
Packit 67cb25
    {
Packit 67cb25
      /* See Knuth's "Art of Computer Programming" Vol. 2, 3rd
Packit 67cb25
         Ed. p.106 for multiplier. */
Packit 67cb25
Packit 67cb25
      state->mt[i] =
Packit 67cb25
        (1812433253UL * (state->mt[i-1] ^ (state->mt[i-1] >> 30)) + i);
Packit 67cb25
      
Packit 67cb25
      state->mt[i] &= 0xffffffffUL;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->mti = i;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
mt_1999_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  mt_state_t *state = (mt_state_t *) vstate;
Packit 67cb25
  int i;
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    s = 4357;   /* the default seed is 4357 */
Packit 67cb25
Packit 67cb25
  /* This is the October 1999 version of the seeding procedure. It
Packit 67cb25
     was updated by the original developers to avoid the periodicity
Packit 67cb25
     in the simple congruence originally used.
Packit 67cb25
Packit 67cb25
     Note that an ANSI-C unsigned long integer arithmetic is
Packit 67cb25
     automatically modulo 2^32 (or a higher power of two), so we can
Packit 67cb25
     safely ignore overflow. */
Packit 67cb25
Packit 67cb25
#define LCG(x) ((69069 * x) + 1) &0xffffffffUL
Packit 67cb25
Packit 67cb25
  for (i = 0; i < N; i++)
Packit 67cb25
    {
Packit 67cb25
      state->mt[i] = s & 0xffff0000UL;
Packit 67cb25
      s = LCG(s);
Packit 67cb25
      state->mt[i] |= (s &0xffff0000UL) >> 16;
Packit 67cb25
      s = LCG(s);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->mti = i;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* This is the original version of the seeding procedure, no longer
Packit 67cb25
   used but available for compatibility with the original MT19937. */
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
mt_1998_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  mt_state_t *state = (mt_state_t *) vstate;
Packit 67cb25
  int i;
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    s = 4357;   /* the default seed is 4357 */
Packit 67cb25
Packit 67cb25
  state->mt[0] = s & 0xffffffffUL;
Packit 67cb25
Packit 67cb25
#define LCG1998(n) ((69069 * n) & 0xffffffffUL)
Packit 67cb25
Packit 67cb25
  for (i = 1; i < N; i++)
Packit 67cb25
    state->mt[i] = LCG1998 (state->mt[i - 1]);
Packit 67cb25
Packit 67cb25
  state->mti = i;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static const gsl_rng_type mt_type =
Packit 67cb25
{"mt19937",                     /* name */
Packit 67cb25
 0xffffffffUL,                  /* RAND_MAX  */
Packit 67cb25
 0,                             /* RAND_MIN  */
Packit 67cb25
 sizeof (mt_state_t),
Packit 67cb25
 &mt_set,
Packit 67cb25
 &mt_get,
Packit 67cb25
 &mt_get_double};
Packit 67cb25
Packit 67cb25
static const gsl_rng_type mt_1999_type =
Packit 67cb25
{"mt19937_1999",                /* name */
Packit 67cb25
 0xffffffffUL,                  /* RAND_MAX  */
Packit 67cb25
 0,                             /* RAND_MIN  */
Packit 67cb25
 sizeof (mt_state_t),
Packit 67cb25
 &mt_1999_set,
Packit 67cb25
 &mt_get,
Packit 67cb25
 &mt_get_double};
Packit 67cb25
Packit 67cb25
static const gsl_rng_type mt_1998_type =
Packit 67cb25
{"mt19937_1998",                /* name */
Packit 67cb25
 0xffffffffUL,                  /* RAND_MAX  */
Packit 67cb25
 0,                             /* RAND_MIN  */
Packit 67cb25
 sizeof (mt_state_t),
Packit 67cb25
 &mt_1998_set,
Packit 67cb25
 &mt_get,
Packit 67cb25
 &mt_get_double};
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_mt19937 = &mt_type;
Packit 67cb25
const gsl_rng_type *gsl_rng_mt19937_1999 = &mt_1999_type;
Packit 67cb25
const gsl_rng_type *gsl_rng_mt19937_1998 = &mt_1998_type;
Packit 67cb25
Packit 67cb25
/* MT19937 is the default generator, so define that here too */
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_default = &mt_type;
Packit 67cb25
unsigned long int gsl_rng_default_seed = 0;