Blame rng/ranlux.c

Packit 67cb25
/* rng/ranlux.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * 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.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
/* This is a lagged fibonacci generator with skipping developed by Luescher.
Packit 67cb25
   The sequence is a series of 24-bit integers, x_n, 
Packit 67cb25
Packit 67cb25
   x_n = d_n + b_n
Packit 67cb25
Packit 67cb25
   where d_n = x_{n-10} - x_{n-24} - c_{n-1}, b_n = 0 if d_n >= 0 and
Packit 67cb25
   b_n = 2^24 if d_n < 0, c_n = 0 if d_n >= 0 and c_n = 1 if d_n < 0,
Packit 67cb25
   where after 24 samples a group of p integers are "skipped", to
Packit 67cb25
   reduce correlations. By default p = 199, but can be increased to
Packit 67cb25
   365.
Packit 67cb25
Packit 67cb25
   The period of the generator is around 10^171. 
Packit 67cb25
Packit 67cb25
   From: M. Luescher, "A portable high-quality random number generator
Packit 67cb25
   for lattice field theory calculations", Computer Physics
Packit 67cb25
   Communications, 79 (1994) 100-110.
Packit 67cb25
Packit 67cb25
   Available on the net as hep-lat/9309020 at http://xxx.lanl.gov/
Packit 67cb25
Packit 67cb25
   See also,
Packit 67cb25
Packit 67cb25
   F. James, "RANLUX: A Fortran implementation of the high-quality
Packit 67cb25
   pseudo-random number generator of Luscher", Computer Physics
Packit 67cb25
   Communications, 79 (1994) 111-114
Packit 67cb25
Packit 67cb25
   Kenneth G. Hamilton, F. James, "Acceleration of RANLUX", Computer
Packit 67cb25
   Physics Communications, 101 (1997) 241-248
Packit 67cb25
Packit 67cb25
   Kenneth G. Hamilton, "Assembler RANLUX for PCs", Computer Physics
Packit 67cb25
   Communications, 101 (1997) 249-253  */
Packit 67cb25
Packit 67cb25
static inline unsigned long int ranlux_get (void *vstate);
Packit 67cb25
static double ranlux_get_double (void *vstate);
Packit 67cb25
static void ranlux_set_lux (void *state, unsigned long int s, unsigned int luxury);
Packit 67cb25
static void ranlux_set (void *state, unsigned long int s);
Packit 67cb25
static void ranlux389_set (void *state, unsigned long int s);
Packit 67cb25
Packit 67cb25
static const unsigned long int mask_lo = 0x00ffffffUL;  /* 2^24 - 1 */
Packit 67cb25
static const unsigned long int mask_hi = ~0x00ffffffUL;
Packit 67cb25
static const unsigned long int two24 = 16777216;        /* 2^24 */
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    unsigned int i;
Packit 67cb25
    unsigned int j;
Packit 67cb25
    unsigned int n;
Packit 67cb25
    unsigned int skip;
Packit 67cb25
    unsigned int carry;
Packit 67cb25
    unsigned long int u[24];
Packit 67cb25
  }
Packit 67cb25
ranlux_state_t;
Packit 67cb25
Packit 67cb25
static inline unsigned long int increment_state (ranlux_state_t * state);
Packit 67cb25
Packit 67cb25
static inline unsigned long int
Packit 67cb25
increment_state (ranlux_state_t * state)
Packit 67cb25
{
Packit 67cb25
  unsigned int i = state->i;
Packit 67cb25
  unsigned int j = state->j;
Packit 67cb25
  long int delta = state->u[j] - state->u[i] - state->carry;
Packit 67cb25
Packit 67cb25
  if (delta & mask_hi)
Packit 67cb25
    {
Packit 67cb25
      state->carry = 1;
Packit 67cb25
      delta &= mask_lo;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      state->carry = 0;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->u[i] = delta;
Packit 67cb25
Packit 67cb25
  if (i == 0)
Packit 67cb25
    {
Packit 67cb25
      i = 23;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      i--;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->i = i;
Packit 67cb25
Packit 67cb25
  if (j == 0)
Packit 67cb25
    {
Packit 67cb25
      j = 23;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      j--;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->j = j;
Packit 67cb25
Packit 67cb25
  return delta;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static inline unsigned long int
Packit 67cb25
ranlux_get (void *vstate)
Packit 67cb25
{
Packit 67cb25
  ranlux_state_t *state = (ranlux_state_t *) vstate;
Packit 67cb25
  const unsigned int skip = state->skip;
Packit 67cb25
  unsigned long int r = increment_state (state);
Packit 67cb25
Packit 67cb25
  state->n++;
Packit 67cb25
Packit 67cb25
  if (state->n == 24)
Packit 67cb25
    {
Packit 67cb25
      unsigned int i;
Packit 67cb25
      state->n = 0;
Packit 67cb25
      for (i = 0; i < skip; i++)
Packit 67cb25
        increment_state (state);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return r;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static double
Packit 67cb25
ranlux_get_double (void *vstate)
Packit 67cb25
{
Packit 67cb25
  return ranlux_get (vstate) / 16777216.0;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
ranlux_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
Packit 67cb25
{
Packit 67cb25
  ranlux_state_t *state = (ranlux_state_t *) vstate;
Packit 67cb25
  int i;
Packit 67cb25
Packit 67cb25
  long int seed;
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    s = 314159265;      /* default seed is 314159265 */
Packit 67cb25
Packit 67cb25
  seed = s;
Packit 67cb25
Packit 67cb25
  /* This is the initialization algorithm of F. James, widely in use
Packit 67cb25
     for RANLUX. */
Packit 67cb25
Packit 67cb25
  for (i = 0; i < 24; i++)
Packit 67cb25
    {
Packit 67cb25
      unsigned long int k = seed / 53668;
Packit 67cb25
      seed = 40014 * (seed - k * 53668) - k * 12211;
Packit 67cb25
      if (seed < 0)
Packit 67cb25
        {
Packit 67cb25
          seed += 2147483563;
Packit 67cb25
        }
Packit 67cb25
      state->u[i] = seed % two24;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->i = 23;
Packit 67cb25
  state->j = 9;
Packit 67cb25
  state->n = 0;
Packit 67cb25
  state->skip = luxury - 24;
Packit 67cb25
Packit 67cb25
  if (state->u[23] & mask_hi)
Packit 67cb25
    {
Packit 67cb25
      state->carry = 1;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      state->carry = 0;
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
ranlux_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  ranlux_set_lux (vstate, s, 223);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
ranlux389_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  ranlux_set_lux (vstate, s, 389);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
static const gsl_rng_type ranlux_type =
Packit 67cb25
{"ranlux",                      /* name */
Packit 67cb25
 0x00ffffffUL,                  /* RAND_MAX */
Packit 67cb25
 0,                             /* RAND_MIN */
Packit 67cb25
 sizeof (ranlux_state_t),
Packit 67cb25
 &ranlux_set,
Packit 67cb25
 &ranlux_get,
Packit 67cb25
 &ranlux_get_double};
Packit 67cb25
Packit 67cb25
static const gsl_rng_type ranlux389_type =
Packit 67cb25
{"ranlux389",                   /* name */
Packit 67cb25
 0x00ffffffUL,                  /* RAND_MAX */
Packit 67cb25
 0,                             /* RAND_MIN */
Packit 67cb25
 sizeof (ranlux_state_t),
Packit 67cb25
 &ranlux389_set,
Packit 67cb25
 &ranlux_get,
Packit 67cb25
 &ranlux_get_double};
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_ranlux = &ranlux_type;
Packit 67cb25
const gsl_rng_type *gsl_rng_ranlux389 = &ranlux389_type;